Java程序进入while循环后似乎没有响应

3
这是我在这个网站上的第一个问题,如果我发布了错误的内容,我很抱歉,并希望得到反馈!虽然这是一个作业问题,但我似乎无法标记它。
无论如何,代码似乎可以编译通过(使用BlueJ),但当我运行它并进入第一个while循环时就会卡住。我添加了一些输出行以查看问题发生的位置,并且当它进入第一个while循环时的第一个System.out从未发生... JVM继续工作,直到我强制重置它。我认为我的初始while循环应该运行4次,然后使用我已经使用的值退出(5个学生,i从2开始),但它似乎根本没有做任何事情,我不知道我做错了什么。
程序完成后的目的概述。
一系列学生经过一排储物柜。
1.学生打开所有储物柜 2.学生关闭每隔第二个储物柜 3.学生翻转每三个储物柜的状态,依此类推,直到达到学生数量
我意识到我还没有正确设置储物柜标志以进行翻转,并打算在第二个while循环中使用!myBool的变体来实现 - 但首先我想确保我的while循环是否工作。希望我由于长时间盯着它而忽略了一些简单的东西!
import java.util.Arrays;

public class Lockers
{
    public static void main (String[]args)
    {
        // Size of lockerArray
        int arraySize = 5;
        // Set up new lockerArray
        boolean[] lockerArray = new boolean[arraySize];

        // Variable for number of students in the exercise
        int studentNumber = 5;

        System.out.println("Student 1 opens all the lockers.\n");// Outputs, good
        // Student 1 opens all the lockers
        // Boolean values for lockerArray true = OPEN; false = CLOSED
        Arrays.fill(lockerArray, true);
        System.out.println(Arrays.toString(lockerArray)); // Outputs 5 true, good

        // Set the student counter at 2 (Student 1 has already made their pass)
        int i = 2;
        // Loop until you run out of students
        while (i <= studentNumber);
        {
            System.out.println("Student Number " + i + " is making their pass.\n");// NEVER HAPPENS - have to reset JVM to stop program
            // Set up a variable to control the sequence required (i.e., Student 2 every second locker,
            // Student 3 every third locker, etc.
            int j = i;
            while (j <= arraySize);
            {
                System.out.println("Student is changing the status of locker number " + j + ".\n");
                // Reverse the flag at each locker for which the statement is true for this student number
                // Need to reduce the variable by 1 as locker 1 would be sitting in lockerArray[0] position
                lockerArray[j-1] = false;
                // Increment locker number by the value of the student in the sequence
                j = j + i;
            }
            // Increment the student count
            i++;
        }
        // Print the final array status
        System.out.println(Arrays.toString(lockerArray));
    }

}

1
“[tag:homework]”标签被禁用,因为很多人认为不允许发布作业问题。只要提问者展示他们的尝试并展示自己解决问题的努力,作业问题是完全可以的。你已经做到了,所以不用担心! - John Kugelman
哦,太棒了,谢谢!!!去掉分号解决了问题。通常我的问题是忘记它们 :) 这个网站很棒,响应速度惊人。 - Amber
2个回答

8
您的while循环后面有分号。
while (i <= studentNumber);

这会导致无限循环,因为您的i变量无法更改。


-3

除非你想让它无限循环,否则需要在 while 循环周围加上括号 试试这个:

while (i <= studentNumber){
// action
}

1
循环已经有括号了,多余的分号是问题所在。 - John Kugelman
你在说什么?这不是第一个答案... - Makoto
2
如果是这样的话,那将是一个糟糕的点赞理由。 - John Kugelman
@Makoto 这是 TylerRutherford120(回答者)在stackoverflow上的第一个答案 :) - necromancer
@JohnKugelman 如果那是我的原因,我同意 :) 我的意思是回答者在stackoverflow上的第一个答案。它并不完全不正确,采用它甚至将其与提问者的代码进行比较,将立即导致解决方案。(请注意,关闭括号和打开大括号之间缺少空格--这不是好的风格,但对于聚焦于多余的分号非常有效) - necromancer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接