这是程序源码:
public class BeerSong{
public static void main(String[] args){
int beerNumber=99;
String word="bottles";
while(beerNumber>0){
if(beerNumber==1) {
word="bottle";
}
System.out.println(beerNumber+"\t"+word+"\t"+"of beer on the wall.");
System.out.println(beerNumber+" "+word+" "+"of beer.");
System.out.println("Take one down.");
System.out.println("Pass it around.\n");
beerNumber--;
if(beerNumber==0){
System.out.println("No more bottles of beer on the wall.");
}
}
}}
在运行中的输出结果见上传附件中的图片。
【问题详情】
1、System.out.println(beerNumber+"\t"+word+"\t"+"of beer on the wall.");
为什么同一行中输出的两个"\t”间距相差很大,而
System.out.println(beerNumber+" "+word+" "+"of beer.");
这个输出的间隔就一样。
2、这个程序是从99数到0,可是运行中只能显示从59到0,用什么方法可以在运行中扩大显示的范围呢?
文件名字的命名错误。
一个.java文件的名称应该是 public class Test
具有public标识的类名。
eg:
class Abc
{
}
public class Test
{
}
那这个java文件应该是以Test命名,而不是Abc。
// 关于\t输出的间隔不一样
因为一个\t表示八个位,从第一位开始算起,例:"abc\tabc",输出"abc abc",中间就只有五个空格。如你问题所提:"99" + \t + "bottles" + "\t" + "xxx",所以输出:
"99 bottles xxx", 因为"bottles"这个字符串,占了七个位,所以只打印一个空格。
// 这个程序是从99数到0,可是运行中只能显示从59到0,用什么方法可以在运行中扩大显示的范围呢?
你是在cmd里运行的吗?应该有滚动条的,估计你输出的太长了,不清楚怎么样扩大显示范围。
你可以在eclipse里运行,即可。