从键盘输入的学生成绩,找出最高分, 并输出学生成绩等级
成绩 >=最高分-10 等级 A
成绩 >=最高分-20 等级 B
成绩 >=最高分-30 等级 C
其余 等级 D
import java.util.Scanner; public class TestScore { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.println("输入学生个数"); int count = s.nextInt(); int[] scores = new int[count]; int maxSocre = 0; System.out.println("输入成绩"); for (int i = 0; i < scores.length; i++) { int score = s.nextInt(); scores[i] = score; if (scores[i] > maxSocre) { maxSocre = scores[i]; } } // 遍历学生成绩数组, 根据差值 赋予相应等级 ,并输出 System.out.println("最高分为:" + maxSocre); for (int i = 0; i < scores.length; i++) { char level; if (scores[i] >= maxSocre - 10) { level = 'A'; } else if (scores[i] >= maxSocre - 20) { level = 'B'; } else if (scores[i] >= maxSocre - 30) { level = 'C'; } else { level = 'D'; } System.out.println("student " + i + " score is " + scores[i] + " grade is " + level); } } }
运行如下: