Ask simple questions about arrays~~thinks~~

As @Chrisir said, you need to put n = n + 1; after your println().

Let’s imagine n = 2. What you are currently doing is:

  • Fill values[2] with a random number
  • Add 1 to n so n is now 2 + 1 = 3
  • Print in the console the value of values[3]

Since you haven’t defined a value for values[3] yet it will display 0.

What you want is swap the last 2 steps:

  • Fill values[2] with a random value
  • Print values[2] in the console
  • Increase the value of n so n =3 now
2 Likes