包装类的自动包装和拆装

 1 package day1_28;
 2 
 3 import java.util.Scanner;
 4 import java.util.Vector;
 5 
 6 /**
 7  * Vector 向量集合类
 8  * 添加元素  addElement(Object obj)
 9  * 读取元素  Object elementAt(int index)
10  */
11 
12 public class ScoreTest {
13     public static void main(String[] args) {
14         ScoreTest st = new ScoreTest();
15         st.score();
16     }
17 
18     //不提示输入学生人数,按任意负数表示结束成绩输入
19     public void score() {
20 
21         Scanner sc = new Scanner(System.in);
22         Vector v = new Vector();
23         int score;
24         int maxScore = 0;
25         for (; ; ) {
26             System.out.print("请输入学生成绩(负数表示输入结束):");
27             score = sc.nextInt();
28             if (score < 0) {
29                 break;
30             }
31             if (score > 100) {
32                 System.out.println("输入的数据非法,请重新输入");
33                 continue;
34             }
35             v.addElement(score);//自动装箱
36             System.out.println();
37             if (maxScore < score) {
38                 maxScore = score;
39             }
40         }
41 
42         System.out.println("最高分是:" + maxScore);
43 
44         char level;
45         for (int i = 0; i < v.size(); i++) {
46             Object obj = v.elementAt(i);
47             score = (int)obj;//向下转型为Integer,然后自动拆箱
48             if (maxScore - score <= 10) {
49                 level = 'A';
50             } else if (maxScore - score <= 20) {
51                 level = 'B';
52             } else if (maxScore - score <= 30) {
53                 level = 'C';
54             } else {
55                 level = 'D';
56             }
57             System.out.println("学生" + (i + 1) + "的成绩是:" + score
58                     + " 等级是" + level);
59         }
60 
61     }
62 }

 

posted @ 2021-01-28 18:38  dog_IT  阅读(111)  评论(0编辑  收藏  举报