public class BottlesOfBeer {
public static void countdown(int bottles) {
if (bottles > 0) {
System.out.printf("%d bottles of beer on the wall,\n", bottles);
System.out.printf("%d bottles of beer,\n", bottles);
System.out.printf("ya' take one down, ya' pass it around,\n", bottles);
bottles -= 1;
System.out.printf("%s bottles of beer on the wall.\n", bottles);
} else if (bottles == 0) {
System.out.println("No bottles of beer on the wall,");
System.out.println("no bottles of beer,");
System.out.println("ya' can't take one down, ya' can't pass it around,");
System.out.println("'cause there are no more bottles of beer on the wall!");
} else {
System.out.println("Wait, you can't have negative bottles...");
}
public static void main(String args[]) {
int bottles = 99;
countdown(bottles);
}
}
Error: illegal start of expression [Line: 18]
我是Java的新手,我不明白为什么在编译它时会出现此错误。据说这个程序从99到1倒计时,然后当瓶子= 0时再次打印。
答案 0 :(得分:2)
您没有关闭countdown
方法,这使main
方法成为其中的一部分。在主方法上方添加结束}
。
感谢您提醒我手动计算括号的日子....
答案 1 :(得分:0)
你错过了一个结束大括号'}'来关闭你的“倒计时”方法。在第18行添加一个。
答案 2 :(得分:0)
else
阻止
else {
System.out.println("Wait, you can't have negative bottles...");
}
} // add this to fix the compile error
或者您可以省略{}
一行语句
else
System.out.println("Wait, you can't have negative bottles...");
} // close braces of countdown method
答案 3 :(得分:0)
你忘了关闭你的cowntdown
功能,如果你想让你的瓶子变量倒计时,你需要循环它。
public class BottlesOfBeer {
public static void countdown(int bottles) {
for(int i = bottles; i>=0 ; i--){
if (bottles > 0) {
System.out.printf("%d bottles of beer on the wall,\n", bottles);
System.out.printf("%d bottles of beer,\n", bottles);
System.out.printf("ya' take one down, ya' pass it around,\n", bottles);
bottles -= 1;
System.out.printf("%s bottles of beer on the wall.\n", bottles);
} else if (bottles == 0) {
System.out.println("No bottles of beer on the wall,");
System.out.println("no bottles of beer,");
System.out.println("ya' can't take one down, ya' can't pass it around,");
System.out.println("'cause there are no more bottles of beer on the wall!");
} else {
System.out.println("Wait, you can't have negative bottles...");
}
}
}
public static void main(String args[]) {
int bottles = 99;
countdown(bottles);
}
}
答案 4 :(得分:0)
你错过了以下其他声明的大括号:
else {
System.out.println("Wait, you can't have negative bottles...");
}
}// Fix
如果您对编码非常陌生,可以使用Notepad ++。有了这个,你就可以轻松避免这种错误。 快乐学习!