Search code examples
javaarrayssequences

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] + " ");
      }

  }
}

it currently prints out 1 2 3 4 5 6 7 8 9 10, but what would be the quickest way to modify it to print the sequence 0 1 0 1 0 1 0 1 0 1?


Solution

  • Change your assignment line to:

    myNums[i] = i % 2;