按考分对学生排序 Exercise08_03

 1 /**
 2  * @author 冰樱梦
 3  * 时间:2018年12月
 4  * 题目:按考分对学生排序
 5  *
 6  */
 7 public class Exercise08_03 {
 8     public static void main(String args[]) {
 9         int names[]=new int[8];
10         int[] list=new int[8];
11         // Students' answers to the questions
12         char[][] answers = {
13                 {'A', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D'},
14                 {'D', 'B', 'A', 'B', 'C', 'A', 'E', 'E', 'A', 'D'},
15                 {'E', 'D', 'D', 'A', 'C', 'B', 'E', 'E', 'A', 'D'},
16                 {'C', 'B', 'A', 'E', 'D', 'C', 'E', 'E', 'A', 'D'},
17                 {'A', 'B', 'D', 'C', 'C', 'D', 'E', 'E', 'A', 'D'},
18                 {'B', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D'},
19                 {'B', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D'},
20                 {'E', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}};
21 
22         // Key to the questions
23         char[] keys = {'D', 'B', 'D', 'C', 'C', 'D', 'A', 'E', 'A', 'D'};
24 
25         // Grade all answers
26         for (int i = 0; i < answers.length; i++) {
27             // Grade one student
28             int correctCount = 0;
29             for (int j = 0; j < answers[i].length; j++) {
30                 if (answers[i][j] == keys[j])
31                     correctCount++;
32             }
33             list[i]=correctCount;
34             names[i]=i;
35 
36             
37         }
38 
39         BubbleSort(list,names);
40     }
41     
42     //冒泡排序从大到小,稍微修改了一下程序,让名字跟着分数一起排序。
43     public static void BubbleSort(int[] scores,int[] names){
44         for(int i=scores.length-1;i>=0;i--){
45             for(int j=0;j<scores.length-i-1;j++){
46                 if(scores[j]>scores[j+1]){
47                     int temp=scores[j];
48                     scores[j]=scores[j+1];
49                     scores[j+1]=temp;
50                     
51                     int tem=names[j];
52                     names[j]=names[j+1];
53                     names[j+1]=tem;
54                 }
55             }
56         }
57         for(int i=0;i<names.length;i++){
58             System.out.println("Student " + names[i]);
59         }
60         
61     }
62 
63 
64 
65 }
原文地址:https://www.cnblogs.com/cherrydream/p/10174181.html