2016-04-30 8 views
-1

私は現在、ユーザーの入力に応じて対応するメソッドを呼び出すことによって、人の応答を使用して加算、乗算、減算または除算を行う方法とスイッチケースを持つ簡単なプログラムを入力しています。スイッチケースステートメントを使用した数学プログラム

私は三つの問題抱えている:私は、私のswitch文は、プログラムが変数answerを初期化するために私を求めている

  • が動作していないが、私はこれを0に設定すると

    1. を方程式が何であっても常に0の応答を得ます。

    2. 私のメソッドは答えを返していませんが、メソッド内から出力したときに正しい答えが表示されます。ここに私のコードとコンパイルエラーがあります。


    import java.util.Scanner; 
    
    public class Math { 
    
        public static void main (String[] args){ 
    
         //used to import scanner class for keyboard use 
         Scanner kb = new Scanner(System.in); 
    
         //used to hold value of first number 
         int a; 
         //used to hold the value of the second number 
         int b; 
         //used to hold value for response of line 29 
         int d; 
    
         //out put message to user 
         System.out.println("Please enter a whole number between 0 and 100."); 
         a = kb.nextInt(); 
    
         if (a > 100) { 
          System.out.println("The number you entered did not match the criteria, please run the program again."); 
          System.exit(0); 
         } else 
          System.out.println("Please enter another whole number between 0 and 100."); 
    
         b = kb.nextInt(); 
    
         if (b > 100) { 
          System.out.println("The number you entered did not match the criteria, please run the program again."); 
          System.exit(0); 
         } else 
          System.out.println("What would you like to do with these two numbers?"); 
    
         System.out.println("1=Add, 2=Subtraction, 3=Multiply, 4=Divide"); 
    
         d = kb.nextInt(); 
    
          switch(d){ 
    
          case '1': 
           System.out.println("you choose addition!"); 
           add(a, b); 
           break; 
    
          case '2': 
           subtract(a, b); 
           break; 
    
          case '3': 
           multiply(a, b); 
           break; 
    
          case '4': 
           divide(a, b); 
           break; 
    
          default: 
           System.out.println("You did not make a valid choice, please run the program again."); 
           System.exit(0); 
          } 
        } 
    
        public static int add(int x, int y){ 
         int answer; 
         answer = x+y; 
         return answer; 
        } 
    
        public static int subtract(int x, int y){ 
         int answer = x-y; 
         return answer; 
        } 
    
        public static int multiply(int x, int y){ 
         int answer = x*y; 
         return answer; 
        } 
    
        public static int divide(int x, int y){ 
         int answer = x/y; 
         return answer; 
        } 
    
    } 
    
  • +2

    CAPSには投稿しないでください。読みやすいように書式を設定してください。 –

    +0

    [スタイルガイド](https://google.github.io/styleguide/javaguide.html)に従うと、コードを読みやすく、デバッグしやすくなります。 –

    +0

    @KevinHookeそれについて申し訳ありません、私は次回にキャップを解雇することを確認します – Richard

    答えて

    0

    あなたは答え変数を設定することはありません。 add、sub、multiple、およびdivideメソッドはintを返しますが、決して答えに割り当てることはありません。 例:

    あなたはswitch文は次のようになり持っている必要があります
    int answer = 0; 
    answer=add(a, b); 
    
    +0

    ありがとうございます。あなたたちは本当に私を助けました。私のスイッチケースの声明が、それほどシンプルなもののために働いていなかったことに気づかなかった – Richard

    3

    switch (d) 
    { 
        case 1: // look for the int 1 not the char '1' 
         System.out.println("you choose addition!"); 
    
         // also, print out what the methods are returning: 
         System.out.println("The answer is: " + add(a, b)); 
         break; 
        case 2: 
         subtract(a, b); 
         break; 
        case 3: 
         multiply(a, b); 
         break; 
        case 4: 
         divide(a, b); 
         break; 
        default: 
         System.out.println("You did not make a valid choice, please run the program again."); 
         System.exit(0); 
    } 
    

    サイドノート:あなたは整数の除算を行っている、あなたの分割方法で

    • 、どの正確ではありません。 ダブルを使用して除算を行い、ダブルを返すことを検討することをお勧めします。プログラムを終了する前に、例えば

    、スキャナアウトあなたはずもclose

    public static double divide (int x, int y) 
    { 
        double answer = (double) x/(double) y; 
        return answer; 
    } 
    
    関連する問題