c语言输入一个百分制成绩,要求输出成绩的等级‘A’、‘B’、‘C’、‘D’、‘E’。90分以上为‘A’,80-89为‘B’,70-79为‘C’,60-69为‘D’,60分以下为‘E’。
时间: 2023-09-25 16:10:29 浏览: 42
你好!感谢您使用ChitGPT进行对话。
对于您的问题,可以使用以下C语言代码来实现:
```
#include <stdio.h>
int main() {
int score;
printf("请输入学生成绩:");
scanf("%d", &score);
if (score >= 90) {
printf("成绩等级为 A\n");
} else if (score >= 80) {
printf("成绩等级为 B\n");
} else if (score >= 70) {
printf("成绩等级为 C\n");
} else if (score >= 60) {
printf("成绩等级为 D\n");
} else {
printf("成绩等级为 E\n");
}
return 0;
}
```
注意,在输入成绩之前,需要输出提示信息来告诉用户需要输入什么内容。scanf("%d", &score)可以将用户输入的整数赋值给score变量,然后使用if-else语句判断成绩所属的等级,并输出结果。
希望您对这个答案满意。如果您还有其他问题或需要更多帮助,请随时问我。
相关问题
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语言代码,实现输入一个百分制成绩,输出对应的等级。
好的,以下是一段用 C 语言编写的代码,可以实现对输入的百分制成绩进行转换并输出对应的等级:
```c
#include <stdio.h>
int main() {
int score;
printf("请输入百分制成绩:");
scanf("%d", &score);
if(score < 0 || score > 100) {
printf("输入不合法!");
return 0;
}
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;
}
```
这段代码中,我们首先通过 `scanf()` 函数读取用户输入的百分制成绩,然后使用 `if...else` 分支结构对输入的成绩进行判断,并输出对应的成绩等级。请注意,在判断百分制成绩是否合法时,我们使用了 `score < 0 || score > 100` 的条件来进行限制,确保用户输入的成绩在合法范围内。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)