前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >第七章第一题(指定等级)(Assign grades) - 编程练习题答案

第七章第一题(指定等级)(Assign grades) - 编程练习题答案

作者头像
无刺鱼
发布2022-03-29 13:14:38
2950
发布2022-03-29 13:14:38
举报
文章被收录于专栏:许唯宇

(指定等级)编写一个程序,读入学生成绩,获取最髙分best, 然后根据下面的规则陚等级值

• 如果分数>=best-10, 等级为A

• 如果分数>=best-20, 等级为B

• 如果分数>=best-30, 等级为C

• 如果分数>=卜68140, 等级为D

• 其他情况下,等级为F

程序提示用户输入学生总数,然后提示用户输入所有的分数,最后显示等级得出结论。下面

是一个运行示例:

(Assign grades) Write a program that reads student scores, gets the best score, and then assigns grades based on the following scheme:

Grade is A if score is >= best -5

Grade is B if score is >= best -10;

Grade is C if score is >= best -15;

Grade is D if score is >= best -20;

Grade is F otherwise.

The program prompts the user to enter the total number of students, and then prompts the user to enter all of the scores, and concludes by displaying the grades.

Here is a sample run:

代码语言:javascript
复制
Enter the number of students: 4
Enter 4 scores: 40 55 70 58
Student 0 score is 40 and grade is C
Student 1 score is 55 and grade is B
Student 2 score is 70 and grade is A
Student 3 score is 58 and grade is B

下面是参考答案代码:

代码语言:javascript
复制
// https://cn.fankuiba.com
import java.util.Arrays;
import java.util.Scanner;


public class Ans7_1_page235 {
    public static void main(String[] args) {
        String[] grade = {"A","B","C","D","F"};
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the number of students: ");
        int number = input.nextInt();
        System.out.print("Enter " + number + " scores: ");

        int[] score = new int[number];
        for (int i = 0; i < number; i++)
            score[i] = input.nextInt();

        int[] scoreSort = new int[number];
        System.arraycopy(score,0,scoreSort,0,score.length);

        Arrays.sort(scoreSort);

        int maxSort = scoreSort[number-1];
        for (int i = 0; i < number; i++) {
            if (score[i] >= maxSort-10)
                System.out.println("Student " + i + " score is " + score[i] +
                        " and grade is " + grade[0]);
            else if (score[i] >= maxSort-20)
                System.out.println("Student " + i + " score is " + score[i] +
                        " and grade is " + grade[1]);
            else if (score[i] >= maxSort-30)
                System.out.println("Student " + i + " score is " + score[i] +
                        " and grade is " + grade[2]);
            else if (score[i] >= maxSort-40)
                System.out.println("Student " + i + " score is " + score[i] +
                        " and grade is " + grade[3]);
            else
                System.out.println("Student " + i + " score is " + score[i] +
                        " and grade is " + grade[4]);
        }
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/06/12 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档