2017-01-28 8 views
0

選択が完了して完了した後、私のプログラムが無限ループに詰まっています。再起動して、メニューオプションを再度実行する必要があります。どんな助けでも大歓迎です。選択を完了した後、メニュープログラムを再起動させるにはどうすればよいですか?

public static void main(String[] args) { 
    // TODO Auto-generated method stub 
    @SuppressWarnings("resource") 
    Scanner input = new Scanner(System.in); 

    System.out.println("Menu"); 
    System.out.println("1. Add"); 
    System.out.println("2. Subtract"); 
    System.out.println("3. Multiply"); 
    System.out.println("4. Divide"); 
    System.out.println("5. Generate a random number"); 
    System.out.println("6. Quit\n"); 

    System.out.print("What would you like to do? "); 
    int choice = input.nextInt(); 
    int count = 0; 

    while (choice < 1 || choice > 6){ 
     count ++; 
     System.out.println("I'm sorry, " +choice+ " is not a valid option.\n"); 
     System.out.println("Menu"); 
     System.out.println("1. Add"); 
     System.out.println("2. Subtract"); 
     System.out.println("3. Multiply"); 
     System.out.println("4. Divide"); 
     System.out.println("5. Generate a random number"); 
     System.out.println("6. Quit\n"); 
     System.out.print("What would you like to do? "); 
     choice = input.nextInt(); 
     if (choice >= 1 && choice <= 6){ 
      continue; 
     } 
     else if (count == 2){ 
      System.out.println("Please try again later."); 
      System.exit(0); 
      return; 
     } 
     } 


    do 
    { 
     switch (choice) 
     { 
      case 1: choice = 1; 

       System.out.print("What is the first number? "); 
       int firstAdd = input.nextInt(); 
       System.out.print("What is the second number? "); 
       int secondAdd = input.nextInt(); 
       System.out.println("Answer: " +(firstAdd + secondAdd)); 

       break; 

      case 2: choice = 2; 

       System.out.print("What is the first number? "); 
       int firstSub = input.nextInt(); 
       System.out.print("What is the second nubmer? "); 
       int secondSub = input.nextInt(); 
       System.out.println("Answer: " +(firstSub - secondSub)); 

       break; 

      case 3: choice = 3; 

       System.out.print("What is the first number?"); 
       int firstMult = input.nextInt(); 
       System.out.print("What is the second number? "); 
       int secondMult = input.nextInt(); 
       System.out.println("Answer: " +(firstMult * secondMult)); 

       break; 

      case 4: choice = 4; 

       System.out.print("What is the first number? "); 
       double firstDiv = input.nextInt(); 
       System.out.print("What is the second number? "); 
       double secondDiv = input.nextInt(); 

        while (secondDiv == 0){ 
         System.out.println("I'm sorry, you can't divide by zero."); 
         break;} 
        { 

        System.out.println("Answer: " +(firstDiv/secondDiv)); 
        break; 
        } 


      case 5: choice = 5; 

       System.out.println(Math.random() * 10 + 1); 

       break; 

      case 6: choice = 6; 

       System.out.println("Goodbye!"); 

       System.exit(0); 
       return; 
     } 

    }while (choice >=1 && choice <= 6); 



} 

私の本で見つけたいくつかの異なるオプションを試しましたが、他の領域でエラーが増えるようです。私は、「休憩」とは異なる陳述があるかどうかわかりません。このケースを初めて使用したのでメニューを再起動するために使用します。

+4

ようこそスタックオーバーフロー!デバッガの使い方を学ぶ必要があるようです。 [補完的なデバッグ手法](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)にご協力ください。その後も問題が残っている場合は、もう少し詳しくお聞かせください。 –

答えて

1

スキャナのインスタンス化後のすべてをwhileループで囲みます。

すなわち、

FIXED
while (true) { 
    // your code 
    if (choice == 6) 
     break; 
} 

:またdo {} while()ループを除去します。あなたのループは無限です。なぜなら、 "選択"はループ内で変化しないからです。

+0

コードは問題なくコンパイルされます。私が抱えている唯一の問題は、一度選択をして終了すると、同じ選択が再び始まります。たとえば、選択肢1を選択した場合は、追加する2つの数値を入力し、答えが与えられた後、加算選択のために2つの数値を再度入力するよう求められます。 – chimera

+0

あなたは正しいです、私は私の答えを修正しました。 – failedProgrammer

+0

助けてくれてありがとう!私は明らかに物事を過度に複雑にしようとしていました。 – chimera

関連する問題