根据考试成绩的等级打印出分数段,优秀为90以上,良好为80~90,中等为70~79,及格为60~69,60以下为不及格,要求采用switch语句
时间: 2023-05-17 15:05:13 浏览: 47
好的,以下是使用 switch 语句实现根据考试成绩的等级打印出分数段的代码:
```
#include <stdio.h>
int main() {
int score;
printf("请输入考试成绩:");
scanf("%d", &score);
switch (score / 10) {
case 10:
case 9:
printf("优秀\n");
break;
case 8:
printf("良好\n");
break;
case 7:
printf("中等\n");
break;
case 6:
printf("及格\n");
break;
default:
printf("不及格\n");
break;
}
return 0;
}
```
希望能够帮到你!
相关问题
假设考试的分数只能为整数,请用两种多路分支语句实现成绩的五档划分(90~100,优秀;80~89,中等;70~79,良好,60~69,及格;小于60,不及格)
好的,我可以用 Python 来实现这个问题。
第一种多路分支语句是 if-elif-else 语句,代码如下:
```
score = int(input("请输入考试分数:"))
if 90 <= score <= 100:
print("优秀")
elif 80 <= score < 90:
print("中等")
elif 70 <= score < 80:
print("良好")
elif 60 <= score < 70:
print("及格")
else:
print("不及格")
```
第二种多路分支语句是多重条件运算符,代码如下:
```
score = int(input("请输入考试分数:"))
result = "优秀" if 90 <= score <= 100 else \
"中等" if 80 <= score < 90 else \
"良好" if 70 <= score < 80 else \
"及格" if 60 <= score < 70 else \
"不及格"
print(result)
```
以上两种方式都可以实现成绩的五档划分。
C语言中假设考试的分数只能为整数,请用两种多路分支语句实现成绩的五档划分(90~100,优秀;80~89,中等;70~79,良好,60~69,及格;小于60,不及格)
第一种多路分支语句实现:
```c
int score; // 假设分数为整数
// 输入分数
printf("请输入分数:");
scanf("%d", &score);
// 判断分数所属的档次
if (score >= 90 && score <= 100) {
printf("优秀\n");
} else if (score >= 80 && score <= 89) {
printf("中等\n");
} else if (score >= 70 && score <= 79) {
printf("良好\n");
} else if (score >= 60 && score <= 69) {
printf("及格\n");
} else {
printf("不及格\n");
}
```
第二种多路分支语句实现:
```c
int score; // 假设分数为整数
// 输入分数
printf("请输入分数:");
scanf("%d", &score);
// 判断分数所属的档次
switch (score / 10) {
case 10:
case 9:
printf("优秀\n");
break;
case 8:
printf("中等\n");
break;
case 7:
printf("良好\n");
break;
case 6:
printf("及格\n");
break;
default:
printf("不及格\n");
break;
}
```
以上两种方式均可实现成绩的五档划分。第一种方式使用 if-else 语句实现,直接判断分数所属的范围。第二种方式使用 switch-case 语句实现,将分数除以 10 得到十位数,根据十位数来判断分数所属的档次。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)