0
You are given a 3x3 matrix with numbers. Output the numbers of the array, each on a new line.
public class Main { public static void main(String[] args) { int[][] matrix = { {8, 1, 6}, {3, 5, 7}, {4, 9, 0}, }; //output the numbers System.out.println(matrix[0][0]); System.out.println(matrix[0][1]); System.out.println(matrix[0][2]); System.out.println(matrix[1][0]); System.out.println(matrix[1][1]); System.out.println(matrix[1][2]); System.out.println(matrix[2][0]); System.out.println(matrix[2][1]); System.out.println(matrix[2][2]);} } I could make it this way. How it can be done by nested 2 for loops
5 Réponses
+ 5
for(int i =0;i<3;i++)
for(int j = 0;j<3;j++)
System.out.println(matrix[i][j]);
+ 2
public class Main {
public static void main(String[] args) {
int[][] matrix = {
{8, 1, 6},
{3, 5, 7},
{4, 9, 0},
{4, 9, 2}
};
//output the numbers
// returns the length of the rows in the array
int lengthOne = matrix.length;
System.out.println("rwo "+lengthOne);
int lengthTwo = matrix[0].length;
System.out.println("col "+lengthTwo);
for (int x=0; x < lengthOne; x++)
{
for (int y=0; y < lengthTwo ; y++)
{
System.out.println(matrix[x][y]);
}
}
}
}
+ 2
public class Main {
public static void main(String[] args) {
int[][] matrix = {
{8, 1, 6},
{3, 5, 7},
{4, 9, 0},
};
//output the numbers
for (int i=0;i<matrix.length;i++){
for(int j=0;j<matrix.length;j++){
System.out.println(matrix[i][j]);
}
}
}
}
0
public class Main {
public static void main(String[] args) {
int[][] matrix = {
{8, 1, 6},
{3, 5, 7},
{4, 9, 0},
};
//output the numbers
for (int i=0;i<matrix.length;i++){
for(int j=0;j<matrix.length;j++){
System.out.println(matrix[i][j]);
}
}
}
}
Good Luck
0
public class Main {
public static void main(String[] args) {
int[][] matrix = {
{8, 1, 6},
{3, 5, 7},
{4, 9, 0},
};
//output the numbers
for(int arr1= 0;arr1 < matrix.length;arr1++){
for(int arr2= 0;arr2 <3;arr2++){
System.out.print(matrix[arr1][arr2] );
}
System.out.println();
}
}
}