2016-04-07 2 views
-2

私はwhileループの中のステートメントをユーザが4(プログラムを終了する)を入力するまで繰り返す必要がありますが、プログラムを実行しようとすると何も画面に出力されません。それはなぜこれをするのだろうか?この回答はおそらく本当に簡単ですが、どんな助けでも本当に感謝しています!whileループ内でswitch文を使用すると、Javaプログラムに出力がありません。

public class Driver 
{ 
public static void main(String[] args) 
{ 
    Scanner keyboard = new Scanner(System.in); 
    int answer; 
    boolean bool = true; 

    while(bool); 
    { 
     System.out.println("\n\tGeometry Calculator\n" + 
          "\t1. Calculate the Area of a Circle\n" + 
          "\t2. Calculate the Area of a Rectangle\n" + 
          "\t3. Calculate the Area of a Triangle\n" + 
          "\t4. Quit\n"); 
     System.out.print("\tEnter your choice (1-4): "); 
     answer = keyboard.nextInt(); 

     switch(answer) 
     { 
      case 1: 
       System.out.println("\n\tCalculating the area of a circle..."); 
       break; 
      case 2: 
       System.out.println("\n\tCalculating the area of a rectangle..."); 
       break; 
      case 3: 
       System.out.println("\n\tCalculating the area of a triangle..."); 
       break; 
      case 4: 
       System.out.println("\n\tQuiting..."); 
       System.exit(0); 
       break; 
      default: 
       System.out.println("\n\tPlease enter a number between 1 and 4."); 
     } 

     if(answer == 4) 
      bool = false; 
    } 
} 
+7

である必要があります。 'while'ループ条件の後にセミコロンを削除してください。 – rgettman

+0

@rgettmanうわー、それは完璧に動作します、どのような愚かな間違い。ありがとうございました! – thelonewanderer

+0

あなたの 'bool'変数は現在冗長です:' while(true) 'と' if(answer == 4)break'で置き換えることができるだけでなく、 'if(answer == 4)あなたが 'case 4'の' System.exit'を使っているからです。代わりに 'if(answer == 4)'の条件を削除し、 'System.exit(0)'を 'bool = false'に置き換えることもできます。 –

答えて

1

小さな間違いが1つあります。 を追加しました。 whileループ後の。ただそれを削除します。あなたのコードは

while(bool) 
関連する問題