Ich arbeite gerade an einem Programm, das Antworten auf verschiedene mathematische Gleichungen benötigt. Ich habe das Programm funktioniert gut mit der Anzeige richtig und falsch basierend auf dem eingegebenen numerischen Wert. Ich versuche, dem Code, der eine falsche Eingabe erkennt, einen zusätzlichen Aspekt hinzuzufügen und den Benutzer aufzufordern, dass seine Eingabe das falsche Format hat (z. B. wenn der Benutzer den Buchstaben a eingibt, zeigt das Programm "Ihre Antwort erfüllt nicht die Korrektes Format, stellen Sie sicher, dass Ihre Antwort eine Ganzzahl ist "). Ich habe versucht, dies zu tun, wenn Anweisungen, die gleich (==) und nicht gleich (! =) Sind, nur den numerischen Aspekt berücksichtigen. Wie gehe ich mit dieser Form der Fehlererkennung in meinem Code vor? Danke für die Hilfe!Fehlererkennung bei nicht-numerischer Eingabe
Hier ist meine aktuellen Code (Ich weiß, seine unordentliche):
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);
}
Am Nötigste enthalten Code gültig ist. – pepperjack
Sie müssen nicht unbedingt Ihren gesamten Code angeben, aber wie erhalten Sie gerade Eingaben vom Benutzer? Verwenden Sie Scanner.nextInt()? –
Ja, ich benutze Scanner.nextInt(). – Quiplo