C语言中输入成绩后自动判断等级功能的两种实现方式

分析:我们既可以使用else if来进行判断也可以使用switch case来进行判断。
题目要求如下:
输入学生成绩,自动判断等级

成绩 等级
90<=score<=100 A等级
80<=score<90 B等级
70<=score<80 C等级
60<=score<70 D等级
0<score<60 E等级

一、使用switch…case语句判断等级

#include <stdio.h>
int main()
{
	int i;
	scanf("%d",&i);
	i/=10;
	switch(i){
		case 9:
			printf("A");
			break;
		case 8:
			printf("B");
			break;
		case 7:
			printf("C");
			break;
		case 6:
			printf("D");
			break;
		default:
			printf("E");
			break;
	}
	return 0; 
}

运行结果
在这里插入图片描述
二、使用else if语句

#include <stdio.h>
int main()
{
	int score;
	scanf("%d",&score);
	if(score>=90)printf("A");
	else if(score>=80)printf("B");
	else if(score>=70)printf("C");
	else if(score>=60)printf("D");
	else printf("E");
	return 0; 
}

运行结果
在这里插入图片描述

原创文章 55 获赞 17 访问量 3644

猜你喜欢

转载自blog.csdn.net/qq_42942881/article/details/105368164