当进入循环时,Java程序出现一次卡住
这是我在这个网站上的第一个问题,所以我很抱歉,如果我发布的内容不正确,我将非常感谢您的反馈!这是一个家庭作业问题,虽然我似乎不能这样标记它 无论如何,代码似乎编译得很好(使用BlueJ),但在我运行它时,当它进入第一个while循环时,它就卡住了。我添加了一些输出行,以查看问题发生的位置和第一个系统。当它进入第一个while循环时,输出将不会发生。。。JVM只是继续工作,直到我强制它重置。我相信我的初始while循环应该运行4次,然后使用我使用的值退出(5个学生,我从2开始),但它似乎什么都没做,我对我做错了什么感到困惑 项目完成后的计划摘要 一群学生走过一排储物柜当进入循环时,Java程序出现一次卡住,java,while-loop,Java,While Loop,这是我在这个网站上的第一个问题,所以我很抱歉,如果我发布的内容不正确,我将非常感谢您的反馈!这是一个家庭作业问题,虽然我似乎不能这样标记它 无论如何,代码似乎编译得很好(使用BlueJ),但在我运行它时,当它进入第一个while循环时,它就卡住了。我添加了一些输出行,以查看问题发生的位置和第一个系统。当它进入第一个while循环时,输出将不会发生。。。JVM只是继续工作,直到我强制它重置。我相信我的初始while循环应该运行4次,然后使用我使用的值退出(5个学生,我从2开始),但它似乎什么都没做
- 学生1打开所有的储物柜
- 学生2每秒钟关闭一次储物柜
- 学生3将每三个储物柜的状态反转一次,以获得学生人数
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));
}
}
导入java.util.array;
公共储物柜
{
公共静态void main(字符串[]args)
{
//储物柜尺寸
int-arraySize=5;
//建立新的储物柜
boolean[]lockerArray=新的boolean[arraySize];
//练习中学生人数的变量
int studentNumber=5;
System.out.println(“学生1打开所有储物柜。\n”);//输出,很好
//学生1打开所有的储物柜
//lockerArray的布尔值true=打开;false=关闭
数组。填充(锁数组,真);
System.out.println(Arrays.toString(lockerArray));//输出5 true,good
//将学生柜台设置为2(学生1已通过)
int i=2;
//循环,直到学生用完为止
while(i您的while
循环后面有分号
while (i <= studentNumber);
while(i除非while循环无限运行,否则在while循环周围需要括号
试试这个:
while (i <= studentNumber){
// action
}
while(我认为该标签被禁用是因为许多人推断作业问题是不允许的。只要提问者展示他们的尝试并展示他们自己解决问题的努力,作业问题就完全可以了。你做到了,所以不用担心!哦,太好了,谢谢!!!删除分号解决了这个问题。通常我的p问题已经忘记了:)这个网站很棒,响应速度令人难以置信。这个循环已经有括号了。问题在于多余的分号。@no\u-response\u-not\u-upposted:你在说什么?这不是第一个答案……如果是的话,那将是一个可怕的投票理由。@Makoto是Tylerutherford120(回答者的)关于stackoverflow的第一个答案:)@JohnKugelman如果这是我的原因,是的,我同意:)我的意思是回答者关于stackoverflow的第一个答案。这并不完全是错误的,因为采用它或者甚至将它与询问者的代码进行比较会立即找到解决方案。(请注意,右括号和大括号之间缺少空格——不是很好的样式,但可以有效地将多余的分号归零)