为什么我的标志不断设置为假?

时间:2019-11-16 01:46:53

标签: java validation do-while

您好,我正在为用户编写一个简单的程序,以进行3个问题的测试。我正在尝试验证用户输入,但是如果用户进入循环以输入正确的数据,因为他们先前输入了错误的数据,那么他们将无法退出循环。即使有下一个答案是正确的。某些东西将我的标志之一设置为false,我无法弄清楚是什么。我尝试调试它无济于事。

import java.util.Scanner;

public class ALittleQuiz {
private static int correct = 0;
public static void main (String[] args){
    Scanner keyboard = new Scanner(System.in);

    System.out.print("Are you read for the quiz? ");
    keyboard.next();
    System.out.println("Okay, here it comes!");
    questionOne();
    questionTwo();
    questionThree();

    System.out.println("Overall, you got "+correct+" out of 3 correct.");
    System.out.println("Thanks for playing!");
}

public static void questionOne(){

    System.out.println("Q1) What is the capital of Alaska?");
    System.out.println("    1) Melbourne");
    System.out.println("    2) Anchorage");
    System.out.println("    3) Juneau");
    System.out.print("> ");
    getUserInputForQuestionOne();
}

public static void questionTwo(){

    System.out.println("Q2) Can you store the value 'cat' in a variable of type int? ");
    System.out.println("    1) Yes");
    System.out.println("    2) No");
    System.out.print("> ");
    getUserInputForQuestionTwo();
}

public static void questionThree(){

    System.out.println("Q3) What is the result of  9+6/3?");
    System.out.println("    1) 5");
    System.out.println("    2) 11");
    System.out.println("    3) 15/3");
    System.out.print("> ");
    getUserInputForQuestionThree();
}

public static void getUserInputForQuestionOne(){
    int testvar = 0;

        try{
            Scanner inputForQuestionOne = new Scanner(System.in);
            testvar = inputForQuestionOne.nextInt();
            validateUserInputForQuestionOne(testvar);
        } catch (Exception e) {
            System.out.println("Error: invalid entry please try again");
            getUserInputForQuestionOne();
        }
}

public static void getUserInputForQuestionTwo(){
    int testvar = 0;

    try{
        Scanner inputForQuestionTwo = new Scanner(System.in);
        testvar = inputForQuestionTwo.nextInt();
        validateUserInputForQuestionTwo(testvar);
    } catch (Exception e) {
        System.out.println("Error: invalid entry please try again");
        getUserInputForQuestionTwo();
    }
}

public static void getUserInputForQuestionThree(){
    int testvar = 0;

    try{
        Scanner inputForQuestionThree = new Scanner(System.in);
        testvar = inputForQuestionThree.nextInt();
        validateUserInputForQuestionThree(testvar);
    } catch (Exception e) {
        System.out.println("Error: invalid entry please try again");
        getUserInputForQuestionThree();
    }
}

public static void validateUserInputForQuestionOne(int choiceOne){

        if(choiceOne >= 1 && choiceOne <= 3){
            sendResponseForQuestionOneToDetermineIfCorrectOrNot(choiceOne);

        }else {
            System.out.println("Please enter 1, 2 or 3 for your selection");
            getUserInputForQuestionOne();
        }
}

public static void validateUserInputForQuestionTwo(int choiceTwo){

    if(choiceTwo >= 1 && choiceTwo <= 3){
        sendResponseForQuestionTwoToDetermineIfCorrectOrNot(choiceTwo);

    }else {
        System.out.println("Please enter 1 or 2 for your selection");
        getUserInputForQuestionTwo();
    }
}

public static void validateUserInputForQuestionThree(int choiceThree){

    if(choiceThree >= 1 && choiceThree <= 3){
        sendResponseForQuestionThreeToDetermineIfCorrectOrNot(choiceThree);

    }else {
        System.out.println("Please enter 1, 2 or 3 for your selection");
        getUserInputForQuestionThree();
    }
}

public static void sendResponseForQuestionOneToDetermineIfCorrectOrNot(int validChoiceOne){

    switch (validChoiceOne){
        case 1: System.out.println("Sorry, that is not correct\n");
            break;
        case 2: System.out.println("Sorry, that is not correct\n");
            break;
        case 3: System.out.println("That's right\n");
            correct++;
            break;
    }
}

public static void sendResponseForQuestionTwoToDetermineIfCorrectOrNot(int validChoiceTwo){

    switch (validChoiceTwo){
        case 1: System.out.println("Sorry, 'cat' is a string. Ints can only store numbers\n");
            break;
        case 2: System.out.println("That's right\n");
            correct++;
            break;
    }
}

public static void sendResponseForQuestionThreeToDetermineIfCorrectOrNot(int validChoiceThree){

    switch (validChoiceThree){
        case 1: System.out.println("Sorry, that is not correct\n");
            break;
        case 2: System.out.println("That's right\n");
            correct++;
            break;
        case 3: System.out.println("Sorry, that is not correct\n");
            break;
    }
}

}

这是我的终端中正在发生的事情:

Are you ready for a quiz? y
Okay, here it comes!

Q1) What is the capital of Alaska?
    1) Melbourne
    2) Anchorage
    3) Juneau
> 456
Please enter 1, 2 or 3 for your selection
Q1) What is the capital of Alaska?
    1) Melbourne
    2) Anchorage
    3) Juneau
> 3
That's right

Please enter 1, 2 or 3 for your selection
Q1) What is the capital of Alaska?
    1) Melbourne
    2) Anchorage
    3) Juneau
> 2
Sorry, that is not correct

Please enter 1, 2 or 3 for your selection
Q1) What is the capital of Alaska?
    1) Melbourne
    2) Anchorage
    3) Juneau
> 

编辑:因此,阅读其中一篇文章后,我回到了绘图板上,能够解决我的问题。我确实要感谢那些花时间帮助我的人。我更新了代码,我认为它更容易理解并且更容易看。上面是我新创建的代码。如果有人对此有任何说明,我将不胜感激!

2 个答案:

答案 0 :(得分:1)

在questionOne内部,您调用了validateQuestionOneUerInput。在validateQuestionOneUerInput中,如果用户输入的不是1、2或3,则设置flagForQuestionOne,然后再次调用questionOne。无论此调用的结果如何,flagForQuestionOne都是false,因此您现在有一个无限循环。

该方法可以查看用户的响应是否有效,并返回true或false,或者可以为无效输入抛出异常,但不应重新调用questionOne。

答案 1 :(得分:0)

在do while循环中,而不是使用标志,而是在检查标志是否等于== false。

下面是不正确的,因为当您将flag的值设置为true时,while条件的值为false。

while (flag == false);

将其更改为

while(flag)