C语言输入学生的百分制成绩,输出对应的成绩等级,如输入89,输出成绩等级:B。用switch语句做)。
时间: 2024-06-11 17:05:03 浏览: 32
```c
#include <stdio.h>
int main() {
int score;
printf("请输入学生的百分制成绩:");
scanf("%d", &score);
switch (score / 10) {
case 10:
case 9:
printf("成绩等级:A\n");
break;
case 8:
printf("成绩等级:B\n");
break;
case 7:
printf("成绩等级:C\n");
break;
case 6:
printf("成绩等级:D\n");
break;
default:
printf("成绩等级:E\n");
}
return 0;
}
```
相关问题
输入百分制成绩,输出等级成绩(用c语言的switch语句实现)
以下是用C语言的switch语句实现输入百分制成绩,输出等级成绩的代码:
```c
#include <stdio.h>
int main() {
int score;
printf("请输入百分制成绩:");
scanf("%d", &score);
switch (score / 10) {
case 10:
case 9:
printf("等级成绩为:A\n");
break;
case 8:
printf("等级成绩为:B\n");
break;
case 7:
printf("等级成绩为:C\n");
break;
case 6:
printf("等级成绩为:D\n");
break;
default:
printf("等级成绩为:E\n");
break;
}
return 0;
}
```
程序首先提示用户输入百分制成绩,然后通过scanf函数读入该成绩。接着,使用switch语句根据成绩的十位数进行判断,输出相应的等级成绩。注意,在本程序中,当成绩为100分时,也会被判定为A级。
c语言编写输入百分制成绩要求输出成绩等级用switch方法实现
可以参考下面的代码实现:
```
#include <stdio.h>
int main()
{
float score;
char grade;
printf("请输入成绩:");
scanf("%f", &score);
if (score > 100 || score < 0) {
printf("输入错误\n");
return 0;
}
switch ((int)score / 10) {
case 10:
case 9:
grade = 'A';
break;
case 8:
grade = 'B';
break;
case 7:
grade = 'C';
break;
case 6:
grade = 'D';
break;
default:
grade = 'E';
break;
}
printf("成绩等级为:%c\n", grade);
return 0;
}
```
这段代码实现了输入百分制成绩并输出成绩等级的功能,使用了 Switch 语句进行判断。注意判断输入成绩是否在合理范围内,保证程序的健壮性。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)