我仍然是Java的初学者,我对嵌套循环以及如何处理行和列感到困惑。
我的目标是,
enter num: 5
1 10 11 20 21
2 9 12 19 22
3 8 13 18 23
4 7 14 17 24
5 6 15 16 25.
这是到目前为止我的代码,
System.out.print("Enter Number: ");
x = in.nextInt();
for(int a = 0; a < x; a++) //rows
{
for(int b = 0; b < x; b++) //columns
{
if(b % 2 == 0){
} else{
}
}
System.out.println();
}
发布于 2018-09-22 22:21:17
要将矩阵加载到内存中:
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[][] matrix = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
matrix[i][j] = scanner.nextInt();
}
}
System.out.println(Arrays.deepToString(matrix));
https://stackoverflow.com/questions/52457291
复制相似问题