C++中的Switch语句
Switch statement in C++
考虑:
#include <iostream>
using namespace std;
int main()
{
int score;
char grade;
cout << "Enter your score: " << endl;
cin >> score;
if (score >= 90)
grade = 'a';
if (score >= 80)
grade = 'b';
if (score >= 70)
grade = 'c';
if (score >= 60)
grade = 'd';
else
grade = 'f';
cout << grade << endl;
switch (grade) {
case 'a':
cout << "Good job" << endl;
break;
case 'c':
cout << "Fair job" << endl;
break;
case 'f':
cout << "Failure" << endl;
break;
default:
cout << "invalid" << endl;
}
cin.get();
return 0;
}
为什么当我输入95
时,它会给出我的默认切换用例,而我应该得到用例"a"?
您缺少了一堆else
,或者按错误的顺序进行比较。
95大于90,但也大于80、70和60。所以你会得到一个"d"。
(而且您没有在交换机中处理"d"。(
我相信你想要
if (score >= 90)
grade = 'a';
else if (score >= 80)
grade = 'b';
else if (score >= 70)
grade = 'c';
else if (score >= 60)
grade = 'd';
else
grade = 'f';
除了最后两种情况外,你所拥有的并不相互排除任何情况,60及以上和以下。您的代码不会短路;它检查1到5的所有值。
if (score >= 90) // 1.
grade = 'a';
if (score >= 80) // 2.
grade = 'b';
if (score >= 70) // 4.
grade = 'c';
if (score >= 60) // 5.
grade = 'd';
else
grade = 'f';
我认为您希望使用'else-if'。如果";分数>=60〃;这是真的,并且等级等于"0";d";,这将在switch语句中生成默认情况。
您已经指定了它,使您的95满足所有情况:95大于90,但也大于80,大于70等。
在这种情况下,最后一个获胜。
您可以使用else
来解决它,也可以将它封装在一个函数中,并在知道所需等级后立即返回:
char grade(int score) {
if (score >= 90) return 'a';
if (score >= 80) return 'b';
...
}
因为所有的score
比较都没有与if/else if
条件相结合。它们是独立的if
语句。因此CCD_ 7被CCD_。
if分支的顺序错误(或者您需要提供els分支,如下所示:(。
在这里观看直播:http://ideone.com/2uSZT
#include <iostream>
using namespace std;
int main()
{
int score;
char grade;
cout << "Enter your score:" << endl;
cin >> score;
if (score >= 90)
grade = 'a';
else if (score >= 80)
grade = 'b';
else if (score >= 70)
grade = 'c';
else if (score >= 60)
grade = 'd';
else
grade = 'f';
cout << grade << endl;
switch (grade)
{
case 'a':
cout << "Good job" << endl;
break;
case 'c':
cout << "Fair job" << endl;
break;
case 'f':
cout << "Failure" << endl;
break;
default:
cout << "invalid" << endl;
}
cin.get();
return 0;
}
这是因为顶部的if语句。您应该使用else-ifs,而不是单独的ifs。你的iffor 90正在执行,然后所有其他人也在执行。你的信;a";因为95是>=适用于所有其他条件。使用else-if将在找到真检查时中断其余检查。
if (score >= 90)
grade = 'a';
else if (score >= 80)
grade = 'b';
else if (score >= 70)
grade = 'c';
else if (score >= 60)
grade = 'd';
else
grade = 'f';
如果条件允许,您需要改进。
您正在检查score >= no
。输入95
执行所有if语句,最后执行的语句是d
。现在,在switch语句中,case d
不存在,因此它执行default
语句。
你已经得到了一些答案,但我想我会提出一个稍微不同的可能性,它可以去掉大部分控制流,并用一些数学方法代替:
char grades[] = "00000012344";
char *messages[] = {
"Excellent Job",
"Good job",
"Average job",
"Mediocre Job",
"Failure"
};
if (score < 0 || score > 100)
std::cout << "Invalid score";
else {
int grade = grades[score/10];
std::cout << messages[grade];
}
因此,我们使用score/10
将0-100的分数转换为0-10。然后,我们查找适当的分数,其中f=0,d=1,c=2,b=3,a=4。我们使用它来选择并打印出合适的消息。我为你跳过的信件添加了信息(可能不是你想要的(。
- 当调用switch语句中的函数时(即使函数不包含循环),似乎是永不结束的循环的问题
- 如何将 if else 语句重写为 switch 语句
- 将 out/in out 参数与 if/switch 的 init 语句一起使用
- Switch 语句(字符串)一直选择默认值,除非其为零
- C++ switch 语句:对案例进行分组,并针对每个案例给出特定的说明
- 任意大小的 constexpr 数组是否可以用作 switch 语句中的案例?
- 为什么在 C/C++ 中交织 switch/for/if 语句是有效的?
- 默认标签显然在 switch 语句中不起作用
- C++如何在 switch 语句中放置两个参数
- 如何在 c++ 中从 switch 语句返回不同类型的变量
- switch-case语句如何在Arduino/C++中工作
- 在编译时自动生成用于稀疏数组索引的switch语句
- 代码样式:在 switch/if 语句的分支中重用控件表达式或控制变量
- C++ Switch 语句中的数学运算
- 变量不能在 lambda 中隐式捕获,并且没有使用 switch 语句指定捕获默认值
- 在任何案例之前,在 switch 语句中的使用声明是否合法?
- 有没有办法缩短长 switch() 语句?
- 使用 switch 语句将不同的函数作为类属性分配 C++
- 为什么我的 switch 语句没有响应一半的情况?
- 没有if或if相关语句(switch、while、for、?:等)的可视化c++递归