-2

当有人输入字母作为菜单选项时,我想让这个程序输出错误

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");
            }

        }
    }
}
4

3 回答 3

1

如果您想在用户输入正确之前使用循环功能,您可以使用此代码段。

while(true){
   try{
      UserIn = input.nextInt();
      break;
   }catch (Exception ex){
         System.out.println("Not an acceptable input.");
         input.nextLine();
   }
}
于 2012-12-27T01:53:25.123 回答
0

您可以使用“Character.isLetter(char)”检查字符是否为字母

if(Character.isLetter(UserIn))
{
    //do something
}

希望这可以帮助!

(PS:以后请把你的文本格式更清晰一点,并且清楚你的问题到底是什么,不仅在标题栏中,而且在问题的主要内容中。)

于 2012-12-27T01:19:53.293 回答
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");
    }
于 2012-12-27T03:54:04.510 回答