要求:输入5+, 输出100;输入5-,输出90;输入5,输出95。
我的写法如下,输出5的时候必须加一个空格,如何去掉空格呢?请问该如何编写呢?
``` case 5:
switch(b) {
case '+':
printf("The score is 100.\n");
break;
case '-':
printf("The score is 85.\n");
break;
case '0':
printf("The score is 90.\n");
break;
default:
printf("False input.\n");
}
break;
#include
int main(int argc, char *argv[])
{
int a;
char b;
scanf("%d%c",&a,&b); //ab两个输入连续输入到操作台,中间没有符号隔开
switch(a){
case 5:
switch(b) { //先判定a,再判定b,switch的嵌套
case '+':
printf("The score is 100.\n");
break;
case '-':
printf("The score is 85.\n");
break;
case ' ':
printf("The score is 90.\n"); //如果输入的只有5,被判定为错误的输入,所以此处我编写的程序需输入“5”加上“空格”
// 请问老师:如果要实现只输入5,运行就能输出90. case后该如何写?就是声明case后为空。
break;
default:
printf("False input.\n");
}
break;
case 4:
switch(b) {
case '+':
printf("The score is 80.\n");
break;
case '-':
printf("The score is 70.\n");
break;
case ' ':
printf("The score is 75.\n");
break;
default:
printf("False input.\n");
}
break;
case 3:
printf("The score is 60.\n");
break;
case 2:
printf("The score is <60.\n");
break;
case 1:
printf("The score is < 60.\n");
break;
default:
printf("False input!\n");
}
return 0;
}
scanf("%d%c",&a,&b);
修改为scanf("%d",&a);
if (scanf("%c", &b) == 1)
{
switch(b) { //先判定a,再判定b,switch的嵌套
case '+':
printf("The score is 100.\n");
break;
case '-':
printf("The score is 85.\n");
break;
case ' ':
printf("The score is 90.\n"); //如果输入的只有5,被判定为错误的输入,所以此处我编写的程序需输入“5”加上“空格”
// 请问老师:如果要实现只输入5,运行就能输出90. case后该如何写?就是声明case后为空。
break;
}
}
else
{
printf("False input.\n");
}