你将自动播放着名的歌曲“99瓶墙上的XXX”。 您将打印歌曲的所有99节经文的歌词。 使用循环!如果 你不知道歌词,用谷歌查一下。
该计划应该:
询问用户的年龄
如果用户年满21岁或以上,请询问他们是否喜欢啤酒或苏打水
一个。如果他们不满21岁或他们更喜欢苏打水,那么歌词是“99 墙上的苏打水瓶“
湾如果它们超过21,则它是“99瓶啤酒”
你必须使用WHILE LOOP并且计数器变量必须是 打印声明!
所以第一节将是:
墙上有99瓶苏打水
99瓶苏打水如果其中一个瓶子从墙上掉下来
......墙上的.98瓶苏打水
最后一节:
墙上的一瓶苏打水
1瓶苏打水
如果那瓶单独的汽水应该从墙上掉下来
墙上没有苏打水瓶!
所以想想,你需要添加什么来循环打印最后一节经文 歌词略有不同?
//这是我的代码。当我运行它时,最后的歌词显示如下
1 of beer on the wall
1 bottles of beer
If one of those bottles should fall off the wall
...0 bottles of beer on the wall
0 of beer on the wall
0 bottles of beer
If that lone bottle of beer should fall off the wall
No bottles of beer on the wall
//怎么做" 0"瓶子部分不显示??
Scanner user = new Scanner(System.in);
int age, beverage;
System.out.println("Please type in your age");
age = user.nextInt();
user.nextLine();
System.out.println("Would you like soda or beer? soda=1 beer=2");
beverage = user.nextInt();
if(age<21 || beverage==1)
{
int bottles = 99;
while( 1<= bottles){
System.out.println(bottles+" of soda on the wall");
System.out.println(bottles+" bottles of soda");
System.out.println("If one of those bottles should fall off the wall");
bottles--;
System.out.println("..."+bottles+" bottles of soda on the wall");
if(bottles==0){
System.out.println(bottles+" of soda on the wall");
System.out.println(bottles+" bottles of soda");
System.out.println("If that lone bottle of soda should fall off the wall");
System.out.println("No bottles of soda on the wall");
}
}
}
if(age>=21 && beverage == 2)
{
int bottles=99;
while(1<= bottles){
System.out.println(bottles+" of beer on the wall");
System.out.println(bottles+" bottles of beer");
System.out.println("If one of those bottles should fall off the wall");
bottles--;
System.out.println("..."+bottles+" bottles of beer on the wall");
if(bottles==0){
System.out.println(bottles+" of beer on the wall");
System.out.println(bottles+" bottles of beer");
System.out.println("If that lone bottle of beer should fall off the wall");
System.out.println("No bottles of beer on the wall");
}
}
}
}
}
答案 0 :(得分:0)
我相信你的循环测试条件已经逆转了。另外,我会使用Formatter
syntax使用printf
来实现此功能。然后您可以以格式包含饮料。如果我像运行它一样运行它
public static void main(String[] args) {
Scanner user = new Scanner(System.in);
int bottles = 99;
System.out.println("Please type in your age");
int age = user.nextInt();
System.out.println("Would you like soda or beer? soda=1 beer=2");
int beverage = user.nextInt();
String drink = (age >= 21 && beverage == 2) ? "beer" : "soda";
while (bottles > 1) { // <-- swap 1 and bottles.
System.out.printf("%d bottles of %s on the wall%n", bottles, drink);
System.out.printf("%d bottles of %s%n", bottles, drink);
System.out.println("If one of those bottles "
+ "should fall off the wall");
bottles--;
System.out.printf("... %d bottles of %s on the wall.%n", bottles,
drink);
}
System.out.printf("%d bottle of %s on the wall%n", 1, drink);
System.out.printf("%d bottle of %s%n", 1, drink);
System.out.printf("If that lone bottle of %s should fall off the wall%n",
drink);
System.out.printf("No bottles of %s on the wall!%n", drink);
}
答案 1 :(得分:0)
if(age>=21 && beverage==2)
int bottles = 99; /* initialize bottles here*/