C语言成绩90-100分为优秀,70-89分为良好,60-69分为合格,60分以下为不及格。使用switch语句编写程序实现 输入某学生成绩,输出等级。
时间: 2024-06-11 18:08:39 浏览: 7
```
#include <stdio.h>
int main() {
int score;
printf("请输入学生成绩:");
scanf("%d", &score);
switch (score / 10) {
case 10:
case 9:
printf("优秀\n");
break;
case 8:
case 7:
printf("良好\n");
break;
case 6:
printf("合格\n");
break;
default:
printf("不及格\n");
break;
}
return 0;
}
```
相关问题
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语言判断成绩优秀、良好、及格、不及格
在C语言中,我们可以使用条件语句来判断成绩的等级,常用的条件语句有if语句和switch语句。
1. 使用if语句判断成绩等级:
```c
int score = 85; // 假设成绩为85分
if (score >= 90) {
printf("优秀\n");
} else if (score >= 80) {
printf("良好\n");
} else if (score >= 60) {
printf("及格\n");
} else {
printf("不及格\n");
}
```
2. 使用switch语句判断成绩等级:
```c
int score = 85; // 假设成绩为85分
switch (score / 10) {
case 10:
case 9:
printf("优秀\n");
break;
case 8:
printf("良好\n");
break;
case 7:
case 6:
printf("及格\n");
break;
default:
printf("不及格\n");
break;
}
```
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)