第七章第一题(指定等级)(Designated grade)

第七章第一题(指定等级)(Designated grade)

  • *7.1(指定等级)编写一个程序,读入学生成绩,得到最高分best,然后根据下面的规则给出等级值:

    • 如果分数>=best-10,等级为A
    • 如果分数>=best-20,等级为B
    • 如果分数>=best-30,等级为C
    • 如果分数>=best-40,等级为D
    • 其他情况下,等级为F

    程序提示用户输入学生总数,然后提示用户输入所有的分数,最后显示等级给出结论。下面是一个运行示例:
    Enter the number of students: 4
    Enter 4 scores: 40 55 70 58
    Student 0 score is 40.0 and grade is C
    Student 1 score is 55.0 and grade is B
    Student 2 score is 70.0 and grade is A
    Student 3 score is 58.0 and grade is B
    *7.1(Designated grade)Write a program to read the students’ scores, get the highest score of best, and then give the grade value according to the following rules:

    • If the score is > = best-10, the grade is a
    • If the score is > = best-20, the grade is B
    • If the score is > = best-30, the grade is C
    • If the score is > = best-40, the grade is d
    • In other cases, grade F

    The program prompts the user to enter the total number of students, then prompts the user to enter all the scores, and finally displays the grade to give the conclusion. Here is a running example:

  • 参考代码:

package chapter07;

import java.util.Scanner;

public class Code_01 {
    
    
    public static void main(String[] args) {
    
    
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the number of students: ");
        int number = input.nextInt();
        System.out.print("Enter " + number + " scores: ");
        double max = 0;
        double[] str = new double[number];
        for (int i = 0;i < number;i++){
    
    
            double score1 = input.nextDouble();
            str[i] = score1;
            max = max > score1 ? max : score1;
        }
        for (int i = 0;i < number;i++){
    
    
            System.out.println("Student " + i + " score is " + str[i] + " and grade is " + print(str[i],max));
        }
    }
    public static String print(double number,double max){
    
    
        if (number >= max - 10)
            return "A";
        else if (number >= max - 20)
            return "B";
        else if (number >= max - 30)
            return "C";
        else if (number >= max - 40)
            return "D";
        return "F";
    }
}

  • 结果显示:
Enter the number of students: 4
Enter 4 scores: 40 55 70 58
Student 0 score is 40.0 and grade is C
Student 1 score is 55.0 and grade is B
Student 2 score is 70.0 and grade is A
Student 3 score is 58.0 and grade is B

Process finished with exit code 0

猜你喜欢

转载自blog.csdn.net/jxh1025_/article/details/109264034