Chapter 7 Question 1 (Designated grade)

Chapter 7 Question 1 (Designated grade)

  • *7.1 (Specify grade) Write a program, read in student scores, get the highest score best, and then give the grade value according to the following rules:

    • If score>=best-10, grade is A
    • If the score>=best-20, the grade is B
    • If the score >= best-30, the grade is C
    • If the score >= best-40, the grade is D
    • In other cases, the grade is 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 level to give a conclusion. Here is a running example:
    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:

  • Reference Code:

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";
    }
}

  • The results show that:
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

Guess you like

Origin blog.csdn.net/jxh1025_/article/details/109264034
Recommended