一.交换二维数组
代码如下:
public class H {
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr[][]= {{91,25,8},{56,14,2},{47,3,67}};//定义一个二维数组
System.out.println("---原始数组----");
for(int s[]:arr){//遍历数组,输出原始数组
for(int e:s) {
System.out.print(e+" ");
}System.out.println();
}
System.out.println("--调换位置之后的数组--");
for(int i=0;i<arr.length-1;i++) {//外循环,列
for(int j=1;j<arr.length;j++) {//内循环,行
if(i==j) {//如果列和行相等,跳过循环
continue;
}
int e=arr[i][j];//进行交换
arr[i][j]=arr[j][i];
arr[j][i]=e;
}
}
for(int s[]:arr){//遍历数组输出交换后的数组
for(int e:s) {
System.out.print(e+" ");
}System.out.println();
}
}
}
代码运行结果如下:
二.查询成绩
代码:
import java.util.Scanner;//导入扫描器
public class Daan {
public static void main(String[] args) {
// TODO Auto-generated method stub
String a[]= {"B","A","D","C","C","B","C","A","B","B"};
//定义正确答案的数组
String b[][]= {{"D","A","D","D","C","A","C","C","A","B"},
{"B","A","D","C","B","B","A","A","C","C"},
{"B","C","B","D","D","C","A","C","A","B"},
{"C","B","A","A","C","B","C","B","D","A"},
{"D","A","C","C","D","A","D","C","A","C"},
{"A","B","D","D","C","B","C","D","B","B"},
{"C","B","C","A","A","B","D","C","A","D"},
{"D","A","D","D","A","A","C","A","C","B"}};
//定义学生答案数组
System.out.println("您想调取第几位学生的答题结果(有效数字为1至8):");
//信息提示
Scanner q=new Scanner(System.in);//打开扫描器
int w=q.nextInt( );//输入学生学号
if(w<=0||w>8) {//(有效数字为1至8)
System.out.println("不在有效范围内");
return;//返回
}
int d=0;//正确答案的计数器
System.out.println("第"+w+"位同学的全部答案为:");
for(int i=0,f=w-1;i<b[f].length;i++) {
//因为计算机从0开始,所以学号-1
if(b[f][i]==a[i]) {//如果与正确答案一样,计数器加1
d++;
}
System.out.print(b[f][i]+" ");
}
System.out.println();
System.out.print("第"+w+"位同学一共答对了"+d+"道题");
}
}
代码运行结果如下: