java:输入一个3×3的整数矩阵,将它们存入数组a中,交换其第一行和最后一行

java:输入一个3×3的整数矩阵,将它们存入数组a中,交换其第一行和最后一行后,再以矩阵格式输出

第1个回答  2016-11-17
public class Demo {

public static void main(String[] args) {
int [][] a = new int[3][3];

int nTemp = 0;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
nTemp+=1;
a[i][j] = nTemp;
}
}

int [] aryTemp = a[0];
a[0] = a[a.length-1];
a[a.length-1] = aryTemp;

PrintAry(a);
}
public static void PrintAry(int[][] ary){
for (int i = 0; i < ary.length; i++) {
for (int j = 0; j < ary[i].length; j++) {
System.out.print(ary[i][j] + "\t");
}
System.out.println();
}
}
}本回答被提问者和网友采纳
第2个回答  2018-11-28
import java.util.Scanner;
public class w {
public static void main(String[] args) {
int ri, repeat;
int a[][],i,j,t,q,p;
Scanner in=new Scanner(System.in);
repeat=in.nextInt();
for(ri=1; ri<=repeat; ri++){
a=new int[3][3];
for(i=0; i<3; i++)
for(j=0;j<3;j++)
a[i][j]=in.nextInt();
System.out.println(a[2][0]+" "+a[2][1]+" "+a[2][2]);
System.out.println(a[1][0]+" "+a[1][1]+" "+a[1][2]);
System.out.println(a[0][0]+" "+a[0][1]+" "+a[0][2]);
}
}
}
相似回答