当有人输入一个字母作为菜单选项时,我想让这个程序输出错误
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");
}
}
}
}
答案 0 :(得分:1)
如果您希望在用户输入正确的输入之前有一个循环功能,则可以使用此代码段。
while(true){
try{
UserIn = input.nextInt();
break;
}catch (Exception ex){
System.out.println("Not an acceptable input.");
input.nextLine();
}
}
答案 1 :(得分:0)
您可以使用'Character.isLetter(char)'检查字符是否为字母
if(Character.isLetter(UserIn))
{
//do something
}
希望这有帮助!
(PS:请在将来更清楚地说明你的文字,并清楚你的问题究竟是什么,不仅在标题栏中,而且在问题的主要内容中。)
答案 2 :(得分:0)
您只需在代码中应用try catch块就可以在方便时输出错误,如:
try{
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");
}
}
}catch (Exception e) {
System.out.println("Invalid Input !!! Program will exit");
}