当有人输入字母作为菜单选项时,我想让这个程序输出错误
package main;
import java.util.Scanner;
public class Main {
Scanner input = new Scanner(System.in);
public static void main(String[] args) {
int UserIn = 0;
double numberInputA;
double numberInputB;
double Answer;
Scanner input = new Scanner(System.in);
while (UserIn != 5) {
System.out.println("What type of calculation would you like to perform?");
System.out.println("1. Add\n2. Subtract\n" +
"3. Multiply\n4. Divide\n5. Exit");
UserIn = input.nextInt();
if (UserIn > 5) {
System.out.println("ERROR choose the numbers on the menu");
} else if (UserIn < 1) {
System.out.println("ERROR choose the numbers on the menu");
}
if (UserIn < 5) {
System.out.println("Enter first number:");
numberInputA = input.nextDouble();
System.out.println("Enter second number");
numberInputB = input.nextDouble();
if (UserIn == 1) {
Answer = numberInputA + numberInputB;
System.out.println("Number 1 is:" + numberInputA);
System.out.println("Number 2 is:" + numberInputB);
System.out.println("Result is : " + Answer);
System.out.println("");
}
if (UserIn == 2) {
Answer = numberInputA - numberInputB;
System.out.println("Number 1:" + numberInputA);
System.out.println("Number 2:" + numberInputB);
System.out.println("Result is :" + Answer);
System.out.println("");
}
if (UserIn == 3) {
Answer = numberInputA * numberInputB;
System.out.println("Number 1:" + numberInputA);
System.out.println("Number 2:" + numberInputB);
System.out.println("Result is :" + Answer);
System.out.println("");
}
if (UserIn == 4) {
Answer = numberInputA / numberInputB;
System.out.println("Number 1:" + numberInputA);
System.out.println("Number 2:" + numberInputB);
System.out.println("Result is :" + Answer);
System.out.println("");
}
}
if (UserIn == 5) {
System.out.println("Program exited");
}
}
}
}