当用户按"q"然后按"ENTER"时程序运行时退出



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

有一个尝试和捕获方法,但是每当我按Q和ENTER时,它只会显示捕获部分中的内容。

这是代码:

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

如果你想同时接受数字和字母,最好使用 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!");
}

最新更新