java从键盘输入学生成绩,找出最高分,并输出学生成绩等级。

/*从键盘输入学生成绩,找出最高分,并输出学生成绩等级;
成绩 >=最高分-10 等级为A
成绩 >=最高分-20 等级为B
成绩 >=最高分-30 等级为C
其余为 等级为D

提示:先输入学生人数,根据人数创建int数组,存放学生成绩;
*/


package
study01; import java.util.Scanner; public class Score { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("请输入学生人数:"); int length = sc.nextInt(); int[] scores = new int[length]; int maxScore = 0; for (int i = 0; i < scores.length; i++) { scores[i] = sc.nextInt(); if (scores[i] > maxScore) { maxScore = scores[i]; } } System.out.println("最高分" + maxScore); System.out.println("输入的成绩为:"); for (int i = 0; i < scores.length; i++) { char level; if (scores[i] > maxScore - 10) { level = 'A'; } else if (scores[i] > maxScore - 20) { level = 'B'; } else if (scores[i] > maxScore - 30) { level = 'C'; } else { level = 'D'; } System.out.println("student["+i+"] score is "+scores[i]+" grade is "+level); } } }

输出结果如下:

请输入学生人数:6
90
70
80
50
60
40
最高分90
输入的成绩为:
student[0] score is 90 grade is A
student[1] score is 70 grade is C
student[2] score is 80 grade is B
student[3] score is 50 grade is D
student[4] score is 60 grade is D
student[5] score is 40 grade is D

 

 

posted on 2016-12-04 15:04  王月波  阅读(16933)  评论(0编辑  收藏  举报

导航