Java语言程序设计 例题7.1(指定等级)

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

	}
	
}

结果如下:

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

差劲的厉害了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值