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

标签 java if-statement breakpoints

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

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/

相关文章:

java - 导入 csv 文件以启动 Android 应用程序

java - 带有 MySQL 数据库的 spring-data-elasticsearch

swift - 如何减少 if-condition 循环 - Swift

excel - 有条件复制粘贴

ios - 断点调试器命令中的po $ arg1和bt之间有区别吗?

delphi - 如何使用Delphi IDE保存断点?

java - 基于Object.class创建类

java MD5加密减去字节数组中的值

android - Kotlin:将 'cascade if' 替换为 'when' 与其他变量进行比较

c++ - 遇到值时中断