教师解法:
import java.util.Scanner; import java.util.Vector;
public class TestScore { public static void main(String[] args) { //1.创建Vector的对象,用于存储学生成绩;创建Scanner类对象,用于从键盘获取值 Vector vector = new Vector(); Scanner scanner = new Scanner(System.in); int maxScore = 0;//用于记录输入的学生成绩的最高分 System.out.println("请输入学生成绩:"); //2.通过无限循环的方式,依次输入学生的成绩,并保存在vector对象中:调用addElement()方法 for (;;) { int score = scanner.nextInt(); if (score < 0) { break;//当输入负数时,跳出当前循环 } // vector.addElement(new Integer(score)); vector.addElement(score); //3.获取输入的学生成绩的最高分 if (maxScore < score) { maxScore = score; } } //4.获取学生成绩(调用vector的elementAt(int index)),并输出相应的等级 for (int i = 0; i < vector.size(); i++) { Object obj = vector.elementAt(i); int score = (Integer) obj; char level; if (maxScore - score <= 10) { level = 'A'; } else if (maxScore - score <= 20) { level = 'B'; } else if (maxScore - score <= 30) { level = 'C'; } else { level = 'D'; } System.out.println("student[" + i + "]score is " + score + ",level is " + level); } } }