Task: Create an array of all even numbers from 2 to 20 and output the array elements to the screen first on a line, separating one element from another by a space, and then in a column (separating one element from another by the beginning of a new line). Before creating an array, consider what size it will be.

My code is:

public class Task1 { public static void main(String[] args) { int[] mas1 = new int[9]; for (int i = 2; i <= 20; i = i + 2) { System.out.print(i + " "); if (i == 20) { System.out.println(i); System.out.println(); } } for (int i = 2; i <= 20; i = i + 2) { System.out.println(i); } } } 

According to the results, 2 numbers “20” are displayed in the string (everything is in order with the bar). If in the first for I change the condition to i < 20 , it turns out to be nonsense.

What am I doing wrong?

  • one
    1. Array created but not filled. 2. It is necessary to print values ​​from an array. 3. The print function must be the same with the parameter (column / line). - Yura Ivanov

1 answer 1

you duplicate the output command "20", why? remove the line System.out.println (i); after if (i == 20)

  • Thank you very much! Now everything is correct. - Lina Berezovskaya
  • not at all)))))) - Hex Hex Hex