原代码
int beerNum=99;
String word="bottles";
while(beerNum>0) {
if(beerNum==1) {
word="bottle";//单数的瓶子
}
System.out.println(beerNum+" "+word+" of beer on the wall");
System.out.println(beerNum+" "+word+" of beer.");
System.out.println("Take one down.");
System.out.println("Pass it around.");
beerNum = beerNum - 1;
if(beerNum > 0) {
System.out.println(beerNum+" "+word+" of beer on the wall");
}else {
System.out.println("No more bottles of beer on the wall");
}
}
因为当有1个瓶子的时候,会输出bottles
,所以改进了一下
public static void main(String[] args) {
// TODO Auto-generated method stub
int beerNum=99;
String word="bottles";
while(beerNum>0) {
if(beerNum==1) {
word="bottle";//单数的瓶子
}
System.out.println(beerNum+" "+word+" of beer on the wall");
System.out.println(beerNum+" "+word+" of beer.");
System.out.println("Take one down.");
System.out.println("Pass it around.");
beerNum = beerNum - 1;
// if(beerNum > 0) {
// System.out.println(beerNum+" "+word+" of beer on the wall");
// }else {
// System.out.println("No more botles of beer on the wall");
// }
}
System.out.println("No more botles of beer on the wall");
}
将while里面的判断注释掉,0个瓶子的结果拿到后面来
网友评论