「已注销」 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;
    

    }

    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建