用switch语句写一个根据成绩所在区间判断优良差的程序
时间: 2023-03-23 08:04:10 浏览: 110
好的,我可以回答这个问题。以下是一个用 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 ;
}
```
以上程序会根据输入的成绩,判断其所在的区间,并输出相应的评价。例如,输入 85,输出为“良好”。
相关问题
用switch语句写一个成绩等级划分
当我们需要根据不同的条件执行不同的代码块时,可以使用switch语句。以下是一个用switch语句写成绩等级划分的示例代码:
```
#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;
}
```
在这个示例中,我们首先定义了一个整型变量score,然后通过scanf函数从控制台输入成绩。接着,我们使用switch语句根据score的值来判断成绩等级,并输出相应的结果。注意,在每个case分支中,我们使用了break语句来跳出switch语句,否则程序会继续执行下去。
用switch语句请帮我写一个查找成绩等级和等级区间的代码C语言
当然可以,以下是一个使用switch语句查找成绩等级和等级区间的C语言代码:
```c
#include <stdio.h>
int main() {
int score;
printf("请输入成绩:");
scanf("%d", &score);
switch (score / 10) {
case 10:
case 9:
printf("成绩等级为:A\n");
printf("等级区间为:[90, 100]\n");
break;
case 8:
printf("成绩等级为:B\n");
printf("等级区间为:[80, 89]\n");
break;
case 7:
printf("成绩等级为:C\n");
printf("等级区间为:[70, 79]\n");
break;
case 6:
printf("成绩等级为:D\n");
printf("等级区间为:[60, 69]\n");
break;
default:
printf("成绩等级为:E\n");
printf("等级区间为:[0, 59]\n");
break;
}
return 0;
}
```
这个程序会根据用户输入的成绩,使用switch语句判断成绩等级和等级区间,并输出结果。注意,这个程序只是一个简单的示例,实际应用中可能需要更复杂的判断逻辑。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.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)