我正在开展某种数学培训计划。我正在尝试实现一个 for 循环,以便用户选择的每个问题类型都会生成三个问题。如果用户输入正确的变量类型,这将非常有效。但是,如果用户输入错误的变量/变量类型,则会发生奇怪的循环。这很难解释,但如果你获取代码并尝试给它提供错误的输入,你很快就会发现。我不确定这是如何引起的,所以我想我应该在这里发帖。我已注释掉,以便只有一种操作类型(添加)可用,仅用于调试目的。如果您输入的内容与程序期望的内容不同,您将看到。我对循环还很陌生,并且已经尽力尝试了多种想法,但还是不够。谢谢!
// Welcome
System.out.println("Hello and welcome to the Math Trainer!\n======================================");
System.out.println("Which math operation would you like to practice?");
// Print options to select from
System.out.println(" " + "[A]ddition");
System.out.println(" " +"[S]ubtraction");
System.out.println(" " + "[M]ultiplication");
System.out.println(" " + "[D]ivision");
System.out.println(" " + "[R]emainder");
// Ask for user input on which to choose
System.out.print("Enter your choice:" + " ");
String userLetter = stdin.nextLine();
// Calculate random values from seed and shift them within range
for(int count = 0; count < Config.NUMBER_OF_QUESTIONS; count++){
int ran1 = randGen.nextInt(Config.MAX_VALUE - Config.MIN_VALUE + 1);
int ran2 = randGen.nextInt(Config.MAX_VALUE - Config.MIN_VALUE + 1);
int ran1Shift = ran1 + Config.MIN_VALUE;
int ran2Shift = ran2 + Config.MIN_VALUE;
// Initialize different answers per operation
double additionAnswer = (double)ran1Shift + ran2Shift;
double subtractionAnswer = (double)ran1Shift - ran2Shift;
double multiplicationAnswer = (double)ran1Shift * ran2Shift;
double divisionAnswer = (double)ran1Shift / ran2Shift;
double remainderAnswer = (double)ran1Shift % ran2Shift;
// Prompt user with a question based upon random numbers and operation selection
// Presentation of addition problems
if(userLetter.equalsIgnoreCase("a")) {
System.out.print("What is the solution to the problem:" + " " + ran1Shift + " " + "+" + " " + ran2Shift + " = ");
if (stdin.hasNextDouble()) {
double userNum = stdin.nextDouble();
if (userNum == additionAnswer) {
System.out.println("That is correct!");
} else {
System.out.println("The correct solution is: " + additionAnswer + ".");
}
} else {
System.out.println("All solutions must be entered as decimal numbers.");
System.out.println("The correct solution is " + additionAnswer + ".");
}
}
else{
System.out.println("I'm sorry, I only understand choices of: A, S, M, D, or R!");
}
}
// Program exit
System.out.println("======================================");
System.out.println("Thank you for using the Math Trainer!");
}
}
最佳答案
If I enter 'z' instead of a, s , m, d, or r, for example, the program prints the else statement 3 times.
假设3次是因为Config.NUMBER_OF_QUESTIONS
是3,那是因为你执行String userLetter = stdin.nextLine();
outside 循环,因此 userLetter
的值永远不会改变。
如果修复代码的缩进,使 for
循环的范围变得清晰,您会发现需要在循环内移动以下行:
// Ask for user input on which to choose
System.out.print("Enter your choice:" + " ");
String userLetter = stdin.nextLine();
<小时/>
原始答案
hasNextDouble()
不会消耗任何内容,因此当您提出问题并且用户回答我不知道
而不是输入数字时,文本会保留.
当您提出下一个问题时,上一个问题的(错误)答案仍在缓冲区中,并且系统下一个 hasNextDouble()
调用也会失败,依此类推,并且等等,...
这是人们使用扫描仪
的主要缺陷之一。他们忘记了错误处理。
在这种情况下,每行给出一个答案,因此每次阅读答案时,都应该在之后调用 nextLine()
,以丢弃答案后的任何额外文本,或者丢弃如果给出错误的输入,则整行。
关于java - 循环仅打印 else 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35279182/