[Solved] For loops in Java (using Arrays)


Create an array, type int, length 10. Initialize it with the multiple of 2.

In your code snippet you are not Initializing the array but simply printing the values to the console. You’ll have to initialize it using either the loop or as follows:

int[] values = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};

If you want it to be initialized by writing the loop then here is the corrected code snippet:

public static void main (String[] args) throws Exception {
    int[] values = new int[10];
    values[0] = 2;
    System.out.print(values[0]);
    for (int i = 1; i < values.length; i++) {
        values[i] = values[i-1] + 2;
        System.out.print(" " + values[i]);
    }
}

1

solved For loops in Java (using Arrays)