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时再次打印。
最佳答案
您没有关闭countdown
方法,这使得main
方法成为其中的一部分。在main方法上方添加结束}
。
感谢您让我想起手动计算括号的日子。