平常二维数组虽然用的比较少,但还是温习一下吧
1. 定义一个二维数组,用来记录3个学生的java,c#,sql三门功课的成绩,二维数组的一行记录一个人的成绩,要求循环输入,最后输出格式如下:
public static void main(String[] args) {
int[][] scores = new int[3][4];
String name = "";//功课的名字
Scanner scan = null;
for (int i = 0; i < scores.length; i++) {
System.out.println("请输入第"+(i+1)+"个学生的成绩:");
for (int j = 0; j < scores[i].length;) {
scan = new Scanner(System.in);
if (j == 0) {
name = "java";
} else if (j == 1) {
name = "c#";
} else if (j == 2) {
name = "sql";
} else {
name = "html";
}
System.out.println("请输入"+name+"功课的成绩:");
if (scan.hasNextInt()) {//判断是否为int类型的数
scores[i][j] = scan.nextInt();
j++;//只有输入成功时才+1
} else {
System.out.println("输入的不是int类型的数!");
}
}
}
System.out.println("\t\t java \tc# \tsql \thtml");
for (int i = 0; i < scores.length; i++) {
System.out.print("第"+(i+1)+"名学生\t");
for (int j = 0; j < scores[i].length; j++) {
System.out.print(scores[i][j]+" \t");
}
System.out.println();
}
}