gpt4 book ai didi

java - 为什么我的标志总是被设置为 false?

转载 作者:行者123 更新时间:2023-12-02 09:17:38 24 4
gpt4 key购买 nike

您好,我正在编写一个简单的程序,供用户进行 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 都为 false,因此您现在有一个无限循环。

该方法可以查看用户的响应是否有效并返回 true 或 false,或者可能因输入无效而引发异常,但不应重新调用 QuestionOne。

关于java - 为什么我的标志总是被设置为 false?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58886807/

24 4 0