「已注销」 2018-10-14 01:38 采纳率: 33.3%
浏览 2484
已结题

case语句中case的用法

要求:输入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;

  • 写回答

2条回答 默认 最新

  • 「已注销」 2018-10-14 02:08
    关注

    #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;
    

    }

    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料