2017-02-05 12 views
1

プログラム全体を何度も繰り返すのではなく、メソッドを呼び出すjavaで単純な電卓を作成する必要があります。すべての方法が機能し、正しい選択が行われるまで、ユーザーは間違った選択をすることができます。私が抱えている問題は、操作が完了し、答えが与えられた後にケースから脱落しないということです。ユーザーが終了するまで、途中で中断してプログラムを繰り返す方法

package menuDrivenCalculator; 

import java.util.Scanner; 

public class MenuDrivenCalculator { 
static Scanner input = new Scanner(System.in); 

public static void main(String[] args) { 
    // TODO Auto-generated method stub 

     int menuOption = getMenuOption(); 

     while (menuOption < 1 || menuOption > 6) { 
      System.out.println("I'm sorry, " + menuOption + " is not a valid choice. Please try again."); 
      menuOption = getMenuOption(); 
      if (menuOption >= 1 && menuOption <= 6) { 
       break; 
      } 
     } 

     while (menuOption >= 1 && menuOption <= 6) { 

      switch (menuOption) { 
      case 1: 

       System.out.print("What is the first number? "); 
       double operand1 = getOperand(); 
       System.out.print("What is the second number?"); 
       double operand2 = getOperand(); 

       double add = add(operand1, operand2); 
       System.out.println("Your answer is: " + add); 

       break; 

      case 2: 

       System.out.print("What is the first number? "); 
       operand1 = getOperand(); 
       System.out.print("What is the second number?"); 
       operand2 = getOperand(); 

       double subtract = subtract(operand1, operand2); 
       System.out.println("Your answer is: " + subtract); 

       break; 

      case 3: 

       System.out.print("What is the first number? "); 
       operand1 = getOperand(); 
       System.out.print("What is the second number?"); 
       operand2 = getOperand(); 

       double multiply = multiply(operand1, operand2); 
       System.out.println("Your answer is: " + multiply); 
       break; 

      case 4: 

       System.out.print("What is the first number? "); 
       operand1 = getOperand(); 
       System.out.print("What is the second number?"); 
       operand2 = getOperand(); 

       double divide = divide(operand1, operand2); 
       System.out.println("Your answer is: " + divide); 

       break; 

      case 5: 

       System.out.print("What is the lower limit? "); 
       operand1 = getOperand(); 
       System.out.print("What is the upper limit?"); 
       operand2 = getOperand(); 

       double random = random(operand1, operand2); 
       System.out.println("Your answer is: " + random); 

       break; 

      case 6: 
       System.out.println("Goodbye!"); 
       return; 

      } 

     } 
} 

public static int getMenuOption() { 

    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 menuOption = input.nextInt(); 

    return menuOption; 
} 

public static double getOperand() { 

    double operand = input.nextDouble(); 

    return operand; 
} 

public static double add(double operand1, double operand2) { 

    double add = (operand1 + operand2); 

    return add; 
} 

public static double subtract(double operand1, double operand2) { 

    double subtract = (operand1 - operand2); 

    return subtract; 
} 

public static double multiply(double operand1, double operand2) { 

    double multiply = (operand1 * operand2); 

    return multiply; 
} 

public static double divide(double operand1, double operand2) { 

    double divide = 0; 
    if (operand2 == 0) { 
     divide = Double.NaN; 
    } else if (operand2 != 0) { 
     divide = (operand1/operand2); 
    } 
    return divide; 
} 

public static double random(double operand1, double operand2) { 

    double random = Math.random() * operand2 + operand1; 

    return random; 
} 
} 

何が起こっているかは、プログラムの実行を手動で停止するまで、ユーザーに同じ操作の入力を繰り返し求めていることです。私は、さまざまな種類のループに全体を入れてみましたが、何も変わっていません。

+0

あなたの必要性はあまり明確ではありません。 – davidxxx

+0

は 'switch'を含む' while-loop'を削除し、 'switch'のままにしておきます。また、ユーザーが別の操作を実行できるようにするには、 'main'の中にすべてを含めます。 –

答えて

1

操作を実行するコードがループ内にあるので(while (menuOption >= 1 && menuOption <= 6))、プログラムは最後に選択した操作を繰り返し続けます。

getMenuOption()メソッドも含まれているループが必要なので、ユーザーは別の操作を選択できます。

これを行うには、2つのループを別々にするのではなく、1つだけですべてを処理することができます。defaultのケースをswitchの内部で使用することもできます。

私はあなたに完全な解決策を教えてくれませんが、他の具体的な疑問がある場合はお知らせください。

+0

ありがとう!あなたが私に与えた情報は、私の問題を解決するのに十分でした。そして、それは宿題プロジェクトだったので、私に最後の答えを与えてくれていないことに本当に感謝しています。 – chimera

0

これをやり直したくない場合は、switch文をwhileループに置くのはなぜですか? コード中に 'while'を 'if'と置き換えます。

+0

私はすべてを繰り返す必要がある、私はちょうど1つのケースが何度も何度も繰り返すことを望んでいない。それは私が持っている問題です。 – chimera

+0

次に、2つではなく1つのループでコード全体を繰り返すことができるので、getMenuOption()メソッドが繰り返されます。 – affaz

関連する問題