私は現在、ユーザーの入力に応じて対応するメソッドを呼び出すことによって、人の応答を使用して加算、乗算、減算または除算を行う方法とスイッチケースを持つ簡単なプログラムを入力しています。スイッチケースステートメントを使用した数学プログラム
私は三つの問題抱えている:私は、私のswitch文は、プログラムが変数answer
を初期化するために私を求めている
が動作していないが、私はこれを0に設定すると
を方程式が何であっても常に0の応答を得ます。
私のメソッドは答えを返していませんが、メソッド内から出力したときに正しい答えが表示されます。ここに私のコードとコンパイルエラーがあります。
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;
}
}
CAPSには投稿しないでください。読みやすいように書式を設定してください。 –
[スタイルガイド](https://google.github.io/styleguide/javaguide.html)に従うと、コードを読みやすく、デバッグしやすくなります。 –
@KevinHookeそれについて申し訳ありません、私は次回にキャップを解雇することを確認します – Richard