c++ - 开关盒中的多种选项

标签 c++ c

我正在尝试用 C 语言进行测验。有 5 个答案,只有一个是正确的。我正在使用开关。我的疑问是你是否知道正确答案的数量以及正确答案的后果。但另一方面,我需要把其他 4 个都弄错。我怎样才能选择多个答案?是情况1、3、4、5吗?附注还有无效的选项,我需要为每种情况提供一个字符串! pontos 平均点,p1 就是答案。谢谢您

    switch (p1) {
    case 2 :
        correct answer
        pontos = pontos + 1;
        break;
    case 1, 3, 4, 5 :
        wrong answer
        pontos = pontos - 1;
    default :
        Invalid answer
        pontos = pontos - 1;
    }    

最佳答案

您可以使用同一主体堆叠多个 case 语句,如下所示:

switch (foo) {
    case 1:
    case 2:
    case 3:
    case 5: {
        printf("Sorry, wrong answer.");
        break;
    }
    case 4: {
        printf("You got it right!");
        break;
    }
}

或者您可以使用 default 大小写来捕获所有不正确的内容:

switch (foo) {
    case 4: {
        printf("You got it right!");
        break;
    }
    default: {
        printf("Sorry, wrong answer.");
        break;
    }
}

关于c++ - 开关盒中的多种选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53564447/

相关文章:

c - 在 C 中动态存储数组值

带有 Qt 的 C++ 缓冲区

c++ - 轴对齐边界框碰撞 : what sides are colliding

c++ - Qt 向 QMainWindow 添加非菜单栏键盘快捷方式

c - 使用 scanf_s 读取一个字符

c - tsearch 和 tfind

c++ - OpenCV - 寻找轮廓终点?

c++ - 在 Irrlicht 中移动和旋转 SceneNode(Sphere)

c++ - 对于在 C、C++、Linux 应用程序开发方面拥有超过 8 年经验的候选人,我们应该寻找什么?

c - 为什么x或x的补码是-1?