Write Java code to create a 2D array that mimics the one below. Do not use curly brackets to assign values. Instead, use nested loops and if-else or switch.
x |
o |
o |
o |
x |
o |
x |
o |
x |
o |
o |
o |
z |
o |
o |
o |
x |
o |
x |
o |
x |
o |
o |
o |
x |
public class TwoDArray { public static void main(String[] args) { char[][] data = new char[5][5]; for (int i = 0; i < data.length; i++) { for (int j = 0; j < data[i].length; j++) { if (i == 2 && j == 2) { data[i][j] = 'z'; } else if (i == j || i == 4-j) { data[i][j] = 'x'; } else { data[i][j] = 'o'; } } } // print array for (int i = 0; i < data.length; i++) { for (int j = 0; j < data[i].length; j++) { System.out.print(data[i][j] + " "); } System.out.println(); } } }
Get Answers For Free
Most questions answered within 1 hours.