第七章第一題(指定等級(jí))(Assign grades) - 編程練習(xí)題答案

(指定等級(jí))編寫(xiě)一個(gè)程序,讀入學(xué)生成績(jī),獲取最髙分best, 然后根據(jù)下面的規(guī)則陚等級(jí)值

? 如果分?jǐn)?shù)>=best-10, 等級(jí)為A

? 如果分?jǐn)?shù)>=best-20, 等級(jí)為B

? 如果分?jǐn)?shù)>=best-30, 等級(jí)為C

? 如果分?jǐn)?shù)>=卜68140, 等級(jí)為D

? 其他情況下,等級(jí)為F

程序提示用戶(hù)輸入學(xué)生總數(shù),然后提示用戶(hù)輸入所有的分?jǐn)?shù),最后顯示等級(jí)得出結(jié)論。下面

是一個(gè)運(yùn)行示例:

(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:

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

下面是參考答案代碼:

// 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]);
        }
    }
}

適用Java語(yǔ)言程序設(shè)計(jì)與數(shù)據(jù)結(jié)構(gòu)(基礎(chǔ)篇)(原書(shū)第11版)Java語(yǔ)言程序設(shè)計(jì)(基礎(chǔ)篇)(原書(shū)第10/11版)

發(fā)布在博客:(https://cn.fankuiba.com)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容