0
点赞
收藏
分享

微信扫一扫

数组,输入多个成绩输出最大值和等级

穿裙子的程序员 2022-04-13 阅读 67
eclipsejava

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

效果:

         

代码:

import java.util.Scanner;
public final class arrayEx {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Scanner scanner = new Scanner(System.in);
		System.out.print("请输入学生人数:");
		//1.使用Scanner,读取学生个数
		int num = scanner.nextInt();
		
		//创建数组,存储学生成绩:动态初始化
		int[] score = new int[num];
		int max = 0;
		System.out.println("请输入"+num+"个成绩");
		
		//3.给数组元素赋值
		for (int i = 0; i < score.length; i++) {
			score[i] = scanner.nextInt();
			if (score[i]>=max) {
				max = score[i];
			}
		}
		//4.最大值
		System.out.println("最高分是:"+max);

		char[] grade = new char[num];
		
		//5.根据插值,得到等级
		for (int i = 0; i < score.length; i++) {
			if (score[i]>=max-10) {
				grade[i]='A';
			}else if (score[i]>=max-20) {
				grade[i]='B';
			}else if (score[i]>=max-30) {
				grade[i]='C';
			}else {
				grade[i]='D';
			}
			System.out.println("student "+i+" score is "+score[i]+" ,grade is "+grade[i]);
		}
	}

}

      

举报

相关推荐

0 条评论