gpt4 book ai didi

java - while循环是无限的,因为断点不起作用

转载 作者:行者123 更新时间:2023-11-30 06:45:46 26 4
gpt4 key购买 nike

我正在尝试下面的代码,但遇到了无限循环。断点似乎根本没有帮助。

import java.util.Scanner;

public class Question2 {

public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);

System.out.print("Enter ID Number: ");
int studentSn = keyboard.nextInt();

System.out.print("Enter your Marks: ");
int Score = keyboard.nextInt();

Scanner scan = new Scanner(System.in);
boolean stop = false;

String answer = "";
String Grade = "";
String msg = "";
int counter = 0;

while (!stop) {

if (Score < 50) {
Grade = "F";
} else if (Score <= 64) {
Grade = "P";
} else if (Score <= 74) {
Grade = "C";
} else if (Score <= 84) {
Grade = "D";
} else if (Score <= 100) {
Grade = "HD";
} else {
msg = "Invalid Input";

}

if (Grade != null) {
System.out.println("Student Serial Number: " + studentSn);
System.out.println("Your Grade is: " + Grade);
System.out.println("Do you want to continue (yes/no): " + answer);
} else {
System.out.println("Student Serial Number: " + studentSn);
System.out.println(msg);
System.out.println("Do you want to continue (yes/no): " + answer);
}

while (answer.equalsIgnoreCase("YES"));
{
counter++;

if (answer.equalsIgnoreCase("NO")) {
break;
}
}
}
}
}

最佳答案

在这个场景中有多个无限循环。与

while (!stop) {
// ...
}

您永远不会将“stop”设置为 true,这意味着循环将会结束。

中的 break 语句
while (answer.equalsIgnoreCase("YES"));
{
counter++;

if (answer.equalsIgnoreCase("NO")) {
break;
}
}

只会跳出那个循环,而不是 !stop 循环。如果你想跳出这两个循环,你需要做

MY_LABEL: while (!stop) {
// ...
while (answer.equalsIgnoreCase("YES"));
{
counter++;

if (answer.equalsIgnoreCase("NO")) {
break MY_LABEL;
}
}

或者在某个时候写stop = true;。但是,这并不是代码中唯一的无限循环。在

while (answer.equalsIgnoreCase("YES"));
{
// loop body ^ problem here
}

您的循环语句后跟一个分号!这应该是

while (answer.equalsIgnoreCase("YES"))
{
// loop body
}

因为你现在的代码和写是一样的

while (answer.equalsIgnoreCase("YES"))
; // do nothing
// loop body

因为 Java 的语法是如何工作的。您当前的代码可以编译,因为您可以拥有一个没有任何循环或 if 语句的 block

// do some stuff
{ // begin new scope
int x = 10;
} // end scope
int y = x; // error because x is not in scope!

但这显然不是您想要的。

除此之外,您永远不会将任何内容读入 answer,这意味着它始终等于 ""——它根本不等于“YES”或“NO”!至少在某个地方你应该说

answer = scan.nextLine();

读取输入。

虽然整个程序有点不稳定。以下是我的布局方式:

// Instead of using "stop", we can just break out of the loop when we're done
while(true) {
// ...

// Prompt for input. I use "print" instead of "println" so that the user's answer will be on the same line as the question, e.g.
// Do you want to continue (yes/no): YES
// instead of
// Do you want to continue (yes/no):
// YES
// and so forth
System.out.print("Do you want to continue (yes/no): ");
// This is how we actually read input.
String answer = scan.nextLine();

// If the user doesn't say "YES" (this could be "NO" or "q" or "asdf" or anything else), break out of the loop
if(!answer.equalsIgnoreCase("YES"))
break;
}

我认为您对循环和输入的工作方式有点困惑。仅仅因为您编写了 System.out.println("my question: "+ answer) 并不意味着 Java 会将行的其余部分读入 answer。例如,它实际上会写入 answer 中已有的内容

answer = "abc"
System.out.println("Question? " + answer);
// "Question? abc" will be printed, and no input will be read
// answer still equals "abc"

此外,如果您想重复提问,则必须将所有提问都放到循环中。在您再次readLine() 之前,Java 不会将任何新内容读取到answer 中,所以我认为这就是while 循环引起混淆的原因。在您的 answer.equalsIgnoreCase("YES") 循环中,除非您将 answer = scan.readLine() 放入其中,否则不会读取任何新内容。

关于java - while循环是无限的,因为断点不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48374105/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com