描述
给定正整数 n,以 n 为边长,请通过输出语句按序号从左到右,从上到下输出,形成正方形矩阵,序号从 1 开始,两个序号间用一个空格分隔,每行结尾不加空格。
思路:
- 循环嵌套循环用于处理行列问题
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// write your code here
// read data from console
// output the answer to the console according to the
// requirements of the question
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int m = 1;
/* for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(m % n == 0){
System.out.println(m);
m++;
}else{
System.out.print(m+" ");
m++;
}
}
}*/
//高效一些
for(int i=1;i<=n;i++){
for(int j=1;j<n;j++){
System.out.print(m+" ");
m++;
}
System.out.println(m);
m++;
}
}
}