image.png

    1. package com.atguigu.exercise6;
    2. import java.util.Scanner;
    3. import java.util.Vector;
    4. public class ScoreTest {
    5. public static void main(String[] args) {
    6. //1.实例化Scanner,用于从键盘获取学生成绩
    7. Scanner scan = new Scanner(System.in);
    8. //2.创建Vector对象:Vector v = new Vector();相当于原来的数组
    9. Vector v = new Vector();
    10. //3.通过for(;;)或while(true)方式,给Vector中添加数组
    11. int maxScore = 0;
    12. for(;;){
    13. System.out.println("请输入学生成绩(以负数代表结束)");
    14. int score = scan.nextInt();
    15. //3.2当输入是负数时,跳出循环
    16. if(score < 0){
    17. break;
    18. }
    19. if(score > 100){
    20. System.out.println("输入的数据非法,请重新输入");
    21. continue;
    22. }
    23. //3.1添加操作:v.addElement(Object obj)
    24. // //JDK5.0之前:
    25. // Integer inScore = new Integer(score);
    26. // v.addElement(inScore);//多态
    27. //JDK5.0之后:
    28. v.addElement(score);//自动装箱
    29. //4.获取学生成绩的最大值
    30. if(maxScore < score){
    31. maxScore = score;
    32. }
    33. }
    34. //5.遍历Vector,得到每个学生的成绩,并与最大成绩比较,得到每个学生的等级
    35. char level;
    36. for(int i = 0;i<v.size();i++){
    37. Object obj = v.elementAt(i);
    38. //JDK5.0之前:
    39. // Integer inScore = (Integer)obj;
    40. // int score = inScore.intValue();
    41. //JDk5.0之后
    42. int score = (int)obj;
    43. //比较等级
    44. if(maxScore - score <= 10){
    45. level = 'A';
    46. }else if(maxScore - score <= 20){
    47. level = 'B';
    48. }else if(maxScore - score <= 30){
    49. level = 'C';
    50. }else{
    51. level = 'D';
    52. }
    53. System.out.println("student-"+ i + "score is "+ score + ",level is" + level);
    54. }
    55. }
    56. }