我正在尝试用 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/