*7.1 (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 - 10
Grade is B if score is >= best - 20;
Grade is C if score is>=best - 30;
Grade is D if score is>= best - 40;
Grade is F otherwise.
The program prompts the user to enter the total number of students, 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
*7.1(指定等级)编写一个程序,读取学生的分数,获取最高分,然后根据以下方案分配分数:
Grade is A if score is >= best - 10
Grade is B if score is >= best - 20;
Grade is C if score is>=best - 30;
Grade is D if score is>= best - 40;
程序提示用户输入学生总数,然后提示用户输入所有分数,最后显示成绩,下面是一个例子:
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
代码如下:
import java.util.Scanner;
public class Unite7Test1 {
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
System.out.println("Enter the number of students : ");
int num=scan.nextInt();
System.out.println("Enter "+num+" scores :");
int max=0;
int arr[]=new int[num];
for(int i=0;i<num;i++)
{
int score =scan.nextInt();
arr[i]=score;
if(score>max)
{
max=score;
}
}
for(int i=0;i<arr.length;i++)
{
if(arr[i]>=max-10)
{
System.out.println("Student "+i+" score is "+arr[i]+"and grade is A" );
}else if(arr[i]>=max-20)
{
System.out.println("Student "+i+" score is "+arr[i]+"and grade is B" );
}else if(arr[i]>=max-30)
{
System.out.println("Student "+i+" score is "+arr[i]+"and grade is C" );
}else if(arr[i]>=max-40)
{
System.out.println("Student "+i+" score is "+arr[i]+"and grade is D" );
}else {
System.out.println("Student "+i+" score is "+arr[i]+"and grade is F" );
}
}
}
}
结果如下: