8 个解决方案
#1
这里的问题出在case条件,case 的条件必须是要个常量,而且在C语言中也没有90~100这样的表达方式。这里还是推荐使用if else的判断语句来做
另外提供一下switch的用法
if (grade >= 90 && grade <= 100)
printf("A");
else if (grade >=80 && grade < 90)
printf("B");
另外提供一下switch的用法
//switch的用法
switch (X) {
case 1:
//do something
break;
case 2:
//do something
break;
default:
//do something
}
#2
#include <stdio.h>
int main(void)
{
int grade, ch;
scanf("%d", &grade);
if (grade < 0 || grade > 100)
return 0;
ch = grade / 10;
switch (ch) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
printf("E\n");
break;
case 6:
printf("D\n");
break;
case 7:
printf("C\n");
break;
case 8:
printf("B\n");
break;
case 9:
case 10:
printf("A\n");
break;
default:
printf("Enter error data!\n");
break;
}
return 0;
}
可以参考一下
#3
谢谢,可是我需要将分数段输入到case里要怎么做,不太清楚
#4
谢谢,我if语句会用,但就是case这里不知道如何去写,因为我要输入一个分数段,不太会写
#5
这是两个代码的对比谢谢帮忙
#6
#include <stdio.h>
int main(void)
{
int grade;
//scanf("%d\n", &grade); //不能加\n
scanf("%d", &grade);
switch (grade)
{
case 100:
case 99:
case 98:
case 97:
case 96:
case 95:
case 94:
case 93:
case 92:
case 91:
case 90:
printf("A\n");
break;
case 89:
case 88:
case 87:
case 86:
case 85:
case 84:
case 83:
case 82:
case 81:
case 80:
printf("B\n");
break;
case 79:
case 78:
case 77:
case 76:
case 75:
case 74:
case 73:
case 72:
case 71:
case 70:
printf("C\n");
break;
case 69:
case 68:
case 67:
case 66:
case 65:
case 64:
case 63:
case 62:
case 61:
case 60:
printf("D\n");
break;
case 59:
case 58:
//其他自己写,一般有区间的不宜用switch case语句表达,而应该使用if else语句
case 1:
case 0:
printf("E\n");
break;
}
return 0;
}
#7
我明白了谢谢,刚刚接触C语言,不懂的有很多,还请多多帮忙
#8
谢谢指教,我明白了,下次还请多多帮忙
#1
这里的问题出在case条件,case 的条件必须是要个常量,而且在C语言中也没有90~100这样的表达方式。这里还是推荐使用if else的判断语句来做
另外提供一下switch的用法
if (grade >= 90 && grade <= 100)
printf("A");
else if (grade >=80 && grade < 90)
printf("B");
另外提供一下switch的用法
//switch的用法
switch (X) {
case 1:
//do something
break;
case 2:
//do something
break;
default:
//do something
}
#2
#include <stdio.h>
int main(void)
{
int grade, ch;
scanf("%d", &grade);
if (grade < 0 || grade > 100)
return 0;
ch = grade / 10;
switch (ch) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
printf("E\n");
break;
case 6:
printf("D\n");
break;
case 7:
printf("C\n");
break;
case 8:
printf("B\n");
break;
case 9:
case 10:
printf("A\n");
break;
default:
printf("Enter error data!\n");
break;
}
return 0;
}
可以参考一下
#3
谢谢,可是我需要将分数段输入到case里要怎么做,不太清楚
#4
谢谢,我if语句会用,但就是case这里不知道如何去写,因为我要输入一个分数段,不太会写
#5
这是两个代码的对比谢谢帮忙
#6
#include <stdio.h>
int main(void)
{
int grade;
//scanf("%d\n", &grade); //不能加\n
scanf("%d", &grade);
switch (grade)
{
case 100:
case 99:
case 98:
case 97:
case 96:
case 95:
case 94:
case 93:
case 92:
case 91:
case 90:
printf("A\n");
break;
case 89:
case 88:
case 87:
case 86:
case 85:
case 84:
case 83:
case 82:
case 81:
case 80:
printf("B\n");
break;
case 79:
case 78:
case 77:
case 76:
case 75:
case 74:
case 73:
case 72:
case 71:
case 70:
printf("C\n");
break;
case 69:
case 68:
case 67:
case 66:
case 65:
case 64:
case 63:
case 62:
case 61:
case 60:
printf("D\n");
break;
case 59:
case 58:
//其他自己写,一般有区间的不宜用switch case语句表达,而应该使用if else语句
case 1:
case 0:
printf("E\n");
break;
}
return 0;
}
#7
我明白了谢谢,刚刚接触C语言,不懂的有很多,还请多多帮忙
#8
谢谢指教,我明白了,下次还请多多帮忙