java输入一个整数打印,假如输入4打印如下: 1 2 3 4 8 7 6 5 9 10 11 12 16 15 14 13
时间: 2023-05-22 12:05:49 浏览: 81
U10316005_HW4_11_10:编写一个测试程序,提示用户输入五个字符串并以相反的顺序显示它们
以下是 Java 代码实现:
```
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.print("请输入一个整数: ");
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[][] matrix = new int[n][n];
int num = 1;
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
for (int j = 0; j < n; j++) {
matrix[i][j] = num++;
}
} else {
for (int j = n - 1; j >= 0; j--) {
matrix[i][j] = num++;
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
}
```
如果输入4,则输出如下:
```
1 2 3 4
8 7 6 5
9 10 11 12
16 15 14 13
```
阅读全文