1 import java.util.*; 2 3 public class guanlixiton { 4 5 public static void main(String[] args) { 6 Scanner in = new Scanner(System.in); 7 System.out.println("请输入学生的人数:"); 8 int person = in.nextInt(); // 存储学生的人数 9 System.out.println("请输入课程数目:"); 10 int courseNum = in.nextInt(); // 存储课程的数目 11 String[] name = new String[person]; // 声明一个String数组name存储学生的姓名 12 String[] course = new String[courseNum]; // 声明一个String数组course存储课程的名称 13 int[][] number = new int[person][courseNum]; 14 int[] sum = new int[person];// 声明sum数组来存储总分 15 int[] arg = new int[person];// 声明arg数组来存储平均分 16 String[] str = new String[person]; 17 /* 18 * 用来循环存储课程的名称 19 */ 20 for (int i = 0; i < course.length; i++) { 21 System.out.println("请定义第" + (i + 1) + "课程的名字:"); 22 course[i] = in.next(); 23 } 24 /* 25 * 用来录入学生各科的成绩 26 */ 27 for (int i = 0; i < person; i++) { 28 int s = 0; 29 System.out.println("请输入第" + (i + 1) + "个同学的名字:"); 30 name[i] = in.next(); 31 String str11 = ""; 32 for (int j = 0; j < courseNum; j++) { 33 System.out.println("请输入" + name[i] + "的" + course[j] + "成绩"); 34 number[i][j] = in.nextInt(); 35 s += number[i][j]; 36 str11 += number[i][j] + "\t"; 37 } 38 sum[i] = s;// 求出这同学的总分存入总分sum数组 39 arg[i] = s / courseNum;// 求出这同学的平均分存入平均分arg数组 40 str[i] = name[i] + "\t" + str11 + sum[i] + "\t" + arg[i];
// 整合每行成一个字符串存入字符串数组str 41 } 42 for (int i = 0; i < sum.length - 1; i++) { 43 for (int j = 0; j < sum.length - 1; j++) { 44 if (sum[j] < sum[j + 1]) { 45 int t1 = sum[j]; 46 String t2 = str[j]; 47 sum[j] = sum[j + 1]; 48 str[j] = str[j + 1]; 49 sum[j + 1] = t1; 50 str[j + 1] = t2; 51 } 52 } 53 } 54 /* 55 * 效果输出 56 */ 57 System.out.print("学生"); 58 for (int i = 0; i < course.length; i++) { 59 System.out.print("\t" + course[i]); 60 } 61 System.out.println("\t总分\t平均分\t排行榜"); 62 for (int i = 0; i < person; i++) { 63 System.out.println(str[i] + "\t第" + (i + 1) + "名"); 64 } 65 } 66 67 }