好的,我将在最后发布我所拥有的 Java 代码,但这就是我需要它做的事情。在 main 方法中,声明了一个名为 Lights 的布尔数组,它贯穿 how_many(声明为 int)。
然后我必须调用一个名为initialize的方法,我将在灯光数组中传递灯光数组的大小作为第一个和第二个参数(分别)。
接下来是一个 for 循环,它一遍又一遍地调用方法 do_pass,直到我到达 how_many 的末尾,并将每个数组位置切换为真或假。这将从第二个位置开始,如果 true 将切换为 false,或者如果 false 将切换为 true。它将执行此操作,直到它从 how_many 开始并在 how_many 结束。
然后它将通过调用how_many_are_on方法打印出有多少被切换为真(打开)
到目前为止,这是我的代码:
import java.util.Scanner;
public class Lab7
{
public static void main(String args[])
{
Scanner stdin = new Scanner(System.in);
int how_many = stdin.nextInt();
boolean [] lights = new boolean [how_many];
initialize (lights[0].how_many);
for (int a = 0; a < how_many; a++)
do_pass(lights.length);
}
public static void initialize (boolean [] resets, int size)
{
for (int i = size; i < size; i++)
if (lights[how_many] == true)
lights[how_many] = false;
}
public static void do_pass(boolean lights[], int start, int end, int skip)
{
for (int index = start; index < end; index += skip)
lights[index] = !(lights[index]);
}
public static int how_many_are_on(boolean lights[], int size)
{
int sum = 0;
for (int count = 0; count < size ; count++)
{
if (lights[count] == true)
{
return true;
sum ++;
}
}
return false;
}
}