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