現在、さまざまな数式への回答が必要なプログラムに取り組んでいます。私は入力された数値に基づいて正解と不正確を表示してプログラムを正常に動作させました。私は間違った入力を検出し、入力が間違った形式であることをユーザーに促すコードに余分なアスペクトを追加しようとしています(例えば、ユーザーが文字aを入力すると、プログラムは「あなたの答えは正しい形式で、答えが整数であることを確認してください」)。私はこれをif文で実行しようとしましたが、等しい(==)と等しくない(!=)は数値的な面しか処理しません。私のコードでこの形式のエラー検出を行うにはどうすればいいですか?助けてくれてありがとう!エラー非数値入力の検出
はここに私の現在のコードです(私はその乱雑を知っている):あなたはおそらくa.compareTo(b)
見ている
Random random = new Random();
int a = random.nextInt(10);
int b = random.nextInt(10);
int c = random.nextInt(4);
int d = 0;
int e = 0;
int f = 0;
Scanner input = new Scanner(System.in);
System.out.println("Would you like to choose your math problems, or random problems?");
System.out.println("Input 'randomize' for random problems and 'choose' to choose your operator.");
Scanner inputproblem = new Scanner(System.in);
f = inputproblem.nextInt();
int randomize = 1;
int choose = 2;
if(f == 1){
System.out.println("What operator type would you like? (add,subtract,multiply,divide)");
Scanner operatortype = new Scanner(System.in);
int add = 0;
int subtract = 1;
int multiply = 2;
int divide = 3;
int g = operatortype.nextInt();
if (g == 0){
System.out.print("\t\t\t\tWhat is ");
System.out.print(a);
System.out.print(" + ");
d=a+b;
System.out.print(b+"?");
e = input.nextInt();
input.close();
if (e == d){
System.out.print("\t\t\t\tCorrect!");
}
else if(e != d){
System.out.println("\t\t\t\tIncorrect.");
System.out.println("\t\t\t\tThe correct answer is: "+d);
}
else if (g == 1){
System.out.print("\t\t\t\tWhat is ");
System.out.print(a);
System.out.print(" - ");
d=a-b;
System.out.print(b+"?");
e = input.nextInt();
input.close();
if (e == d){
System.out.print("\t\t\t\tCorrect!");
}
else if(e != d){
System.out.println("\t\t\t\tIncorrect.");
System.out.println("\t\t\t\tThe correct answer is: "+d);
}
else if (g == 2){
System.out.print("\t\t\t\tWhat is ");
System.out.print(a);
System.out.print(" * ");
d=a*b;
System.out.print(b+"?");
e = input.nextInt();
input.close();
if (e == d){
System.out.print("\t\t\t\tCorrect!");
}
else if(e != d){
System.out.println("\t\t\t\tIncorrect.");
System.out.println("\t\t\t\tThe correct answer is: "+d);
}
else if (g == 3){
System.out.print("\t\t\t\tWhat is ");
System.out.print(a);
System.out.print("/");
d=a/b;
System.out.print(b+"?");
e = input.nextInt();
input.close();
if (e == d){
System.out.print("\t\t\t\tCorrect!");
}
else if(e != d){
System.out.println("\t\t\t\tIncorrect.");
System.out.println("\t\t\t\tThe correct answer is: "+d);
}
}
else if (f == 2){
System.out.print("\t\t\t\tWhat is ");
System.out.print(a);
//Switch that allows for the random generation of different math operators
switch (c){
case 0:
System.out.print(" + ");
d=a+b;
break;
case 1:
System.out.print(" - ");
d=a-b;
break;
case 2:
System.out.print("/");
d=a/b;
break;
case 3:
System.out.print(" * ");
d=a*b;
break;
}
System.out.print(b+"?");
e = input.nextInt();
input.close();
if (e == d){
System.out.print("\t\t\t\tCorrect!");
}
else if(e != d){
System.out.println("\t\t\t\tIncorrect.");
System.out.println("\t\t\t\tThe correct answer is: "+d);
}
を使用することにより、有効なのですかどうかを確認には、あなたのコードが含まれています。 – pepperjack
必ずしもすべてのコードを含める必要はありませんが、現在どのようにユーザーからの入力を受けていますか? Scanner.nextInt()を使用していますか? –
はい、Scanner.nextInt()を使用しています。 – Quiplo