c语言输入成绩评等级
if (条件){ 内容
}else if{ 内容
}else{ 内容
}
1 #include <stdio.h>
2 int main(){
3
4 float score;
5 while(1){
6 printf("Please enter your score:");
7 scanf("%f",&score);
8 if(score>=90&&score<=100){
9 printf("Your score is A!\n");
10 }
11 else if(score>=80&&score<=89){
12 printf("Your score is B!\n");
13 }
14 else if(score>=70&&score<=79){
15 printf("Your score is C!\n");
16 }
17 else if(score>=60&&score<=69){
18 printf("Your score is D!\n");
19 }
20 else if(score>=0&&score<=59){
21 printf("Your score is E!\n");
22
23 }
24 else{
25 printf("error!\n");
26
27 }}
28 return 0;
29 }
~
~
~
~
break 和 default 的区别 :
switch(选择){
case 选择1:内容;
break;//跳出程序
case 选择2:内容;
break;//跳出程序
default :其他的内容;//不满足以上的内容则执行该内容
break;
}
1 #include <stdio.h>
2 int main(){
3
4 int score;
5 scanf("%d", &score);
6 printf("Please enter your score 0-99:");
7 switch (score/10) {
8 case 9 :
9 printf("Your score is A:90-99 !\n");
10 break;
11 case 8 :
12 printf("Your score is B!\n");
13 break;
14 case 7 :
15 printf("Your score is C!\n");
16 break;
17 case 6 :
18 printf("Your score is D!\n");
19 break;
20 default :
21 printf("Your score is no no no!\n");
22 break;
23 }
24 return 0;
25 }
~
改进
1 #include <stdio.h>
2 int main() {
3
4 int score;
5 scanf("%d", &score);
6
7 printf("Please enter your score 0-99:");//添加判断 排除错误输入
8 if (score<0 && score>100) {
9 printf("error");
10 }
11
12 switch (score/10) {
13 case 10 :
14 case 9 :
15 printf("Your score is A!\n");
16 break;
17 case 8 :
18 printf("Your score is B!\n");
19 break;
20 case 7 :
21 printf("Your score is C!\n");
22 break;
23 case 6 :
24 printf("Your score is D!\n");
25 break;
26 case 5 :
27 case 4 :
28 case 3 :
29 case 2 :
30 case 1 :
31 case 0 :
32 printf("Your score is D!\n");
33 break;
34
35 }
36 return 0;
37 }
~
再改进
case 90 ... 100 : // ” ... “前后需要有空格 表示90 -- 100 这个区间
1 #include <stdio.h>
2 int main() {
3
4 int score;
5 while (1) {
6 scanf("%d", &score);
7
8
9 switch (score) {
10
11 case 90 ... 100 :
12 printf("Your score is A!\n");
13 break;
14 case 80 ... 89 :
15 printf("Your score is B!\n");
16 break;
17 case 70 ... 79 :
18 printf("Your score is C!\n");
19 break;
20 case 60 ... 69 :
21 printf("Your score is D!\n");
22 break;
23 case 0 ... 59 :
24 printf("Your score is D!\n");
25 break;
26 default :
27 printf("error\n");
28 }
29 }
30 return 0;
31 }
~
标签:case,...,练习,break,score,初学者,printf,Your
From: https://blog.csdn.net/m0_58341340/article/details/142328628