繁体   English   中英

为什么我的标志一直设置为假?

[英]Why does my flag keep getting set to false?

您好,我正在为用户编写一个简单的程序来进行 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
> 

编辑:所以在阅读了你们中的一个人发布的文章后,我回到了绘图板并能够解决我的问题。 我要感谢那些花时间帮助我的人。 我更新了我的代码,我认为它更易于理解和查看。 以上是我新创建的代码。 如果有人对此有任何注释,我将不胜感激!

在 questionOne 中,您调用 validateQuestionOneUerInput。 在 validateQuestionOneUerInput 中,如果用户输入的不是 1、2 或 3,它会设置 flagForQuestionOne,然后再次调用 questionOne。 不管这个调用的结果如何,flagForQuestionOne 都是假的,所以你现在有一个无限循环。

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

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

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

while (flag == false);

将其更改为

while(flag)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM