如何修改此代码以打印以下序列?

时间:2015-02-06 21:43:36

标签: java arrays sequences

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?

1 个答案:

答案 0 :(得分:0)

将作业行更改为:

myNums[i] = i % 2;