2016-09-11 6 views
0
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のときに再び印刷します。main()のJavaで式の開始が正しくありません。

+0

18行目はどちらですか? – Raedwald

答えて

2

メソッドを閉じていないので、mainメソッドがその一部になります。メインメソッドの上にクローズ}を追加します。

と私は手動でカッコを数え日を私に思い出させてくれてありがとう....

0

あなたの「カウントダウン」方式を閉じるために「}」右中括弧を逃しています。 elseブロック

else { 
     System.out.println("Wait, you can't have negative bottles..."); 
    } 
} // add this to fix the compile error 

0

あなたが近くに括弧が欠落しているライン18に1を追加したり、1行の文

あなた cowntdown機能を閉じるのを忘れて
else 
     System.out.println("Wait, you can't have negative bottles..."); 
} // close braces of countdown method 
0

ため{}を省略することができ、ボトルの変数をカウントダウンするには、これをループする必要があります。

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); 
    } 
} 
0

次のelseステートメントの中括弧逃した:あなたは非常に多くの新しいコーディングにある場合は、メモ帳++を使用することができます

else { 
     System.out.println("Wait, you can't have negative bottles..."); 
    } 
}// Fix 

を。これで簡単にそのような間違いを避けることができます。 ハッピーラーニング!

関連する問題