用switch写一个成绩等级
时间: 2024-05-29 14:06:26 浏览: 15
使用switch语句可以根据学生成绩判断其等级,下面是一个简单的示例代码:
```
int score = 85; // 假设学生的分数为85分
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;
}
```
上述代码中,首先根据学生的成绩计算出其所在的十位数(即score/10),然后根据不同的情况输出对应的等级。
相关问题
用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语句写成绩等级
以下是使用switch语句写成绩等级的示例代码:
```python
def get_grade(score):
mark = ""
quotient = score // 10
switch(quotient):
case 9:
case 10:
mark = "A或B"
break
case 8:
mark = "A或B"
break
case 7:
case 7.9:
mark = "C"
break
case 6:
case 6.9:
mark = "D"
break
default:
mark = "F"
return mark
score = 85
grade = get_grade(score)
print("成绩等级为:", grade) # 输出:成绩等级为: A或B
score = 72
grade = get_grade(score)
print("成绩等级为:", grade) # 输出:成绩等级为: C
```
相关推荐
![cpp](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_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)