统计信息

  • 访问数:9422
  • 博客数:142
  • 建立时间:2008-01-05
  • 更新时间:2008-05-21
我叫王超然,是一名电脑爱好者,现在在新加坡留学上高一.我立志成为一名电脑人才,愿意在这里与大家一同分享我玩转电脑的心得.O-level华文考了A-One哈哈!

P6Q21

2008-04-17 22:53:32

天气: 晴朗 心情: 高兴

/*filename:P6Q21.java
 *Name:Wang Chaoran
 *Description:
(Sorting students on grades) Rewrite Listing 6.10, GradeExam.java, to display the students in increasing order of the number of correct answers.
*/
 public class P6Q21 {
   /** Main method */
   public static void main(String args[]) {
    
     // Students' answers to the questions
     char[][] answers = {
       {'A', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D'},
       {'D', 'B', 'A', 'B', 'C', 'A', 'E', 'E', 'A', 'D'},
       {'E', 'D', 'D', 'A', 'C', 'B', 'E', 'E', 'A', 'D'},
       {'C', 'B', 'A', 'E', 'D', 'C', 'E', 'E', 'A', 'D'},
       {'A', 'B', 'D', 'C', 'C', 'D', 'E', 'E', 'A', 'D'},
       {'B', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D'},
       {'B', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D'},
       {'E', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}};
     String[] student=new String[answers.length];
          int[] count=new int[answers.length];
     // Key to the questions
     char[] keys = {'D', 'B', 'D', 'C', 'C', 'D', 'A', 'E', 'A', 'D'};

     // Grade all answers
     for (int i = 0; i < answers.length ; i++) {
       // Grade one student
       int correctCount = 0;
       for (int j = 0; j < answers[i].length ; j++) {
         if (answers[i][j] == keys[j])
           correctCount++;
       }

         student[i] = "Student " + i + "'s correct count is " +correctCount ;
         count[i] += correctCount ;      
     }
 
       quickSort(count,student);
       print(student);
   }
   static void print(String[] array){
   for(int i=0;i<array.length;i++)
     System.out.println(array[i]);
   System.out.println();
   }
   static void print(int[] array){
   for(int i=0;i<array.length;i++)
     System.out.print(array[i]+" ");
     System.out.println();
   }
   static void quickSort(int[] array,String[] student){
    
     for(int i=array.length-1;i>=0;i--){
       int current = array[0];
       String current1=student[0];
     int index =0;
       for(int j=i;j>=0;j--)
       if(current<array[j]){
       current=array[j];
       current1=student[j];
       index=j;
     }
     if(index!=i){
       array[index]=array[i];
       array[i]=current;
       student[index]=student[i];
       student[i]=current1;
     }
   }
 }
  
 }


加入收藏 编辑 审核

TAG: computing

我来说两句

OPEN

Powered by X-Space 1.2 © 2001-2006 Comsenz Technology Ltd