输入无效字符后,询问多项选择题的程序不会向用户显示答案

时间:2019-04-03 01:53:23

标签: java

我正在尝试制作一个程序,其中用户必须回答多项选择问题。当用户输入A,B,C或D时,该程序完全可以正常工作。但是,例如,如果输入“ Z”,则该程序将在特定点停止工作。程序将按照提示进行操作,提示您“无效答案,请输入A,B,C或D。”但是,当您选择A,B,C或D时,程序突然结束,而不显示用户是否正确。

我尝试再次调用getAnswer()方法作为switch语句中的默认情况,因此该程序将再次询问用户他们的选择。在添加这一行之前,它根本没有这样做。但是,它仍未完成最后一步。这是代码:

// Call the method that will ask the user the question. 
        askQuestion();

        // Call the method that will allow the user to answer. 
        getAnswer();

        switch(userChoice)
            {
                case 'a':
                case 'A':
                    System.out.println("Incorrect! 'Switch' IS a key word. The correct answer was B. ");
                    System.out.println("The program will now end. Thanks for answering!");
                    System.exit(0);
                    break;
                case 'b':
                case 'B':
                    System.out.println("Correct!");
                    System.out.println("The program will now end. Thanks for answering!");
                    System.exit(0);
                    break;
                case 'c':
                case 'C':
                    System.out.println("Incorrect! 'Float' IS a key word. The correct answer was B. ");
                    System.out.println("The program will now end. Thanks for answering!");
                    System.exit(0);
                    break;
                case 'd':
                case 'D':
                    System.out.println("Incorrect! 'True' IS a key word. The correct answer was B. ");
                    System.out.println("The program will now end. Thanks for answering!");
                    System.exit(0);
                    break;
                default:
                    System.out.println("Invalid character.");
                    // Repeats the getAnswer method to retrieve a valid character.
                    getAnswer();
            }   
    }

    // method that will ask the question
    public static void askQuestion()  
    {
        // Show the user the possible answers for the multiple choice questions.
        System.out.println("Which of these is NOT a Java key word?");
        System.out.println("A: switch");
        System.out.println("B: keyboard");
        System.out.println("C: float");
        System.out.println("D: true");
    }

    // method that will retrieve the answer from the user 
    public static char getAnswer()
    {
        // create another Scanner object
        Scanner keyboard = new Scanner(System.in);

        // Tell the user to select their answer and store it in a variable. 
        System.out.println("Select your answer by pressing A, B, C, or D.");
        String input = keyboard.nextLine();
        userChoice = input.charAt(0);

        // Return the user's answer to main. 
        return userChoice;
    }

理想情况下,如果用户按下Z,但是下次再次按下A,它将告诉用户错误,但是程序只是结束而不是提供反馈。我想念什么?

4 个答案:

答案 0 :(得分:0)

当收到无效答案时,您可以使用重复方法调用

public void doProcess(){ //this will keep getting called until a valid key is read.

    askQuestion(); 
    userChoice = getAnswer();

    switch(userChoice)
        {
            case 'a':
            case 'A': 
            case 'b':
                //program will exit/ correct wrong ans etc etc 
                break;
            default: 
            //call itself again
            doProcess();
        }
}

答案 1 :(得分:0)

最好在这里使用循环。您一直循环播放,直到用户输入有效的响应为止:

askQuestion(); 

while (true) {

    userChoice = getAnswer();

    switch(userChoice)
    {
        case 'a':
        case 'A':
            System.out.println("Incorrect! 'Switch' IS a key word. The correct answer was B. ");
            System.out.println("The program will now end. Thanks for answering!");
            System.exit(0);

        ...

        default:
            System.out.println("Invalid character.");
    }
}

请注意,在每次 System.exit(0)调用之后,您都不需要 break 语句。

如果您想在错误的输入中再次提出问题,可以将 askQuestion()调用移至 while 循环内。

答案 2 :(得分:0)

您的开关盒仅对第一个答案检查一次。 您可以具有确定答案本身的功能。

导入java.util.Scanner;

公共类主要 {

// method that will ask the question
public static void askQuestion()  
{
    // Show the user the possible answers for the multiple choice questions.
    System.out.println("Which of these is NOT a Java key word?");
    System.out.println("A: switch");
    System.out.println("B: keyboard");
    System.out.println("C: float");
    System.out.println("D: true");
}

// method that will retrieve the answer from the user 
public static char getAnswer()
{
    // create another Scanner object
    Scanner keyboard = new Scanner(System.in);

    // Tell the user to select their answer and store it in a variable. 
    System.out.println("Select your answer by pressing A, B, C, or D.");
    String input = keyboard.nextLine();
    char userChoice = input.charAt(0);

    // Return the user's answer to main. 
    return userChoice;
}


public static void main(String[] args) {// Call the method that will ask the user the question. 
    askQuestion();

    // Call the method that will allow the user to answer. 
    char userChoice = getAnswer();

    while(!checkForCorrectAnswer(userChoice)){
        userChoice = getAnswer();
    }

}

private static boolean checkForCorrectAnswer(char userChoice){
    switch(userChoice)
        {
            case 'a':
            case 'A':
                System.out.println("Incorrect! 'Switch' IS a key word. The correct answer was B. ");
                System.out.println("The program will now end. Thanks for answering!");
                System.exit(0);
                return true;
            case 'b':
            case 'B':
                System.out.println("Correct!");
                System.out.println("The program will now end. Thanks for answering!");
                System.exit(0);
                return true;
            case 'c':
            case 'C':
                System.out.println("Incorrect! 'Float' IS a key word. The correct answer was B. ");
                System.out.println("The program will now end. Thanks for answering!");
                System.exit(0);
                return true;
            case 'd':
            case 'D':
                System.out.println("Incorrect! 'True' IS a key word. The correct answer was B. ");
                System.out.println("The program will now end. Thanks for answering!");
                System.exit(0);
                return true;
            default:
                System.out.println("Invalid character.");
                // Repeats the getAnswer method to retrieve a valid character.
                return false;
        }   
}

}

答案 3 :(得分:0)

为避免不得不使用case表达式来验证它是小写还是大写,建议您使用以下代码:

String value = String.valueOf(userChoice).toUpperCase();

这有助于在转换大小写进行评估之前将小写转换为大写。