Java基礎知識強化之集合框架筆記49:鍵盤錄入5個學生信息(姓名,語文成績,數學成績,英語成績)按照總分從高到低輸出到控制台


1. 鍵盤錄入5個學生信息(姓名,語文成績,數學成績,英語成績)按照總分從高到低輸出到控制台

分析:
   A: 定義學生類
     B: 創建一個TreeSet集合
       C: 總分從高到底如何實現呢?
       D: 鍵盤錄入5個學生信息
       E: 遍歷TreeSet集合

 

2. 代碼示例:

(1)Student.java:

 1 package cn.itcast_08;  2 
 3 public class Student {  4     // 姓名
 5     private String name;  6     // 語文成績
 7     private int chinese;  8     // 數學成績
 9     private int math; 10     // 英語成績
11     private int english; 12 
13     public Student(String name, int chinese, int math, int english) { 14         super(); 15         this.name = name; 16         this.chinese = chinese; 17         this.math = math; 18         this.english = english; 19  } 20 
21     public Student() { 22         super(); 23  } 24 
25     public String getName() { 26         return name; 27  } 28 
29     public void setName(String name) { 30         this.name = name; 31  } 32 
33     public int getChinese() { 34         return chinese; 35  } 36 
37     public void setChinese(int chinese) { 38         this.chinese = chinese; 39  } 40 
41     public int getMath() { 42         return math; 43  } 44 
45     public void setMath(int math) { 46         this.math = math; 47  } 48 
49     public int getEnglish() { 50         return english; 51  } 52 
53     public void setEnglish(int english) { 54         this.english = english; 55  } 56 
57     public int getSum() { 58         return this.chinese + this.math + this.english; 59  } 60 }

 

(2)TreeSetDemo.java:

 1 package cn.itcast_08;  2 
 3 import java.util.Comparator;  4 import java.util.Scanner;  5 import java.util.TreeSet;  6 
 7 /*
 8  * 鍵盤錄入5個學生信息(姓名,語文成績,數學成績,英語成績),按照總分從高到低輸出到控制台  9  * 10  * 分析: 11  * A:定義學生類 12  * B:創建一個TreeSet集合 13  * C:總分從高到底如何實現呢? 14  * D:鍵盤錄入5個學生信息 15  * E:遍歷TreeSet集合 16  */
17 public class TreeSetDemo { 18     public static void main(String[] args) { 19         // 創建一個TreeSet集合
20         TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() { 21  @Override 22             public int compare(Student s1, Student s2) { 23                 // 總分從高到低
24                 int num = s2.getSum() - s1.getSum(); 25                 // 總分相同的不一定語文相同
26                 int num2 = num == 0 ? s1.getChinese() - s2.getChinese() : num; 27                 // 總分相同的不一定數學相同
28                 int num3 = num2 == 0 ? s1.getMath() - s2.getMath() : num2; 29                 // 總分相同的不一定英語相同
30                 int num4 = num3 == 0 ? s1.getEnglish() - s2.getEnglish() : num3; 31                 // 姓名還不一定相同呢
32                 int num5 = num4 == 0 ? s1.getName().compareTo(s2.getName()) 33  : num4; 34                 return num5; 35  } 36  }); 37 
38         System.out.println("學生信息錄入開始"); 39         // 鍵盤錄入5個學生信息
40         for (int x = 1; x <= 5; x++) { 41             Scanner sc = new Scanner(System.in); 42             System.out.println("請輸入第" + x + "個學生的姓名:"); 43             String name = sc.nextLine(); 44             System.out.println("請輸入第" + x + "個學生的語文成績:"); 45             String chineseString = sc.nextLine(); 46             System.out.println("請輸入第" + x + "個學生的數學成績:"); 47             String mathString = sc.nextLine(); 48             System.out.println("請輸入第" + x + "個學生的英語成績:"); 49             String englishString = sc.nextLine(); 50 
51             // 把數據封裝到學生對象中
52             Student s = new Student(); 53  s.setName(name); 54  s.setChinese(Integer.parseInt(chineseString)); 55  s.setMath(Integer.parseInt(mathString)); 56  s.setEnglish(Integer.parseInt(englishString)); 57 
58             // 把學生對象添加到集合
59  ts.add(s); 60  } 61         System.out.println("學生信息錄入完畢"); 62 
63         System.out.println("學習信息從高到低排序如下:"); 64         System.out.println("姓名\t語文成績\t數學成績\t英語成績"); 65         // 遍歷集合
66         for (Student s : ts) { 67             System.out.println(s.getName() + "\t" + s.getChinese() + "\t"
68                     + s.getMath() + "\t" + s.getEnglish()); 69  } 70  } 71 }

運行效果,如下:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM