我如何修改此代码以打印以下序列?
how can i modifty this code to print the following sequence?
public class ArrayR71A
{
public static void main(String [] args)
{
int[] myNums = new int[10];
for (int i = 0; i < myNums.length; i++)
{
myNums[i] = i + 1;
System.out.print(myNums[i] + " ");
}
}
}
它目前打印出 1 2 3 4 5 6 7 8 9 10,但是修改它以打印序列 0 1 0 1 0 1 0 1 0 1 的最快方法是什么?
将您的分配行更改为:
myNums[i] = i % 2;
public class ArrayR71A
{
public static void main(String [] args)
{
int[] myNums = new int[10];
for (int i = 0; i < myNums.length; i++)
{
myNums[i] = i + 1;
System.out.print(myNums[i] + " ");
}
}
}
它目前打印出 1 2 3 4 5 6 7 8 9 10,但是修改它以打印序列 0 1 0 1 0 1 0 1 0 1 的最快方法是什么?
将您的分配行更改为:
myNums[i] = i % 2;