当用户按下' q'当程序运行时退出那么' ENTER'

时间:2018-04-08 12:44:34

标签: java exception try-catch

我希望用户可以选择在程序运行时退出程序,只要他们愿意。例如。随时按Q和ENTER退出并结束程序。

我有一个try和catch方法,但每当我按Q和ENTER时,它只显示catch部分中的内容。

以下是代码:

public static void partB() {
    //Code for partB goes here.
    //Continues on with partA but with few changes
    /* The number of multiplication problems should not be fixed. Instead, 
       the program should keep posing new multiplication problems until the user decides to quit by entering the letter "q".
       The program should be able to deal with invalid input by the user. 
       It should ignore such input and restate the current multiplication problem.
    */                  
    //Uses the imported Random function.
            Random num = new Random();
            //Initialises the minimum and maximum numbers.
            int minNumber = 10; //Minimum number to start random
            int maxNumber = 20; //Maximum number to start random
            int counter = 0; //Counts the number of questions answered.
            int correctAnswers = 0; //Counts the number of correct answers given.
            final int numberOfQuestions = 0;

            while(numberOfQuestions >= 0) {             
            //Generates a random integer between 10 and 20.
                    int randInt1 = (num.nextInt(maxNumber - minNumber) + minNumber);
                    //Repeats for the 2nd integer to get the product of the two numbers.
                    int randInt2 = (num.nextInt(maxNumber - minNumber) + minNumber);

            //Initialise the Scanner function.
            Scanner input = new Scanner(System.in);

            //Output the Question.
            System.out.println("What is " + randInt1 + " X " + randInt2 + "?" + " " + "(Press 'q' and  ENTER to quit)");
            //Waits for user input.
            try {
            int userInput = input.nextInt();
            String quit = input.nextLine();
            //If user input is 'q', quit program.
            if(quit.equalsIgnoreCase("q")) {
                System.out.println("Exiting...");
                System.exit(0);
            } else {

            int answer = randInt1 * randInt2;
            //Checks if the users input is correct.
                if (answer == userInput) {
                    System.out.println("That is correct!");
                    correctAnswers++;
                }
                else {
                    System.out.println("That is incorrect! " + "The correct answer should be " + answer);
                    counter++;
                }
            }

            } catch(InputMismatchException e) {
                System.out.println("You have entered something other than an integer or 'q'! Please try again with a different question!");

        }
    }
}

1 个答案:

答案 0 :(得分:1)

如果您想同时接受数字和字母,最好使用nextLine()。首先检查q,然后解析为数字,如下所示(请注意,parseInt将抛出NumberFormatException):

try {
    String userInput = input.nextLine();
    // If user input is 'q', quit program.
    if (userInput.equalsIgnoreCase("q")) {
        System.out.println("Exiting...");
        System.exit(0);
    } else {

        int userAnswer = Integer.parseInt(userInput);
        int answer = randInt1 * randInt2;
        // Checks if the users input is correct.
        if (answer == userAnswer) {
            System.out.println("That is correct!");
            correctAnswers++;
        } else {
            System.out.println("That is incorrect! " + "The correct answer should be " + answer);
            counter++;
        }
    }

} catch (NumberFormatException e) {
    System.out.println(
        "You have entered something other than an integer or 'q'! Please try again with a different question!");

}