[英]Switch statement in C++
Consider:考虑:
#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;
}
Why is it giving my default switch case when I enter 95
when I should be getting case 'a'?为什么当我输入
95
时会给出我的默认 switch case,而我应该得到 case 'a'?
You're missing a bunch of else
s, or doing the comparisons in the wrong order.你错过了一堆
else
,或者以错误的顺序进行比较。
95 is greater than 90, but it's also greater than 80, 70 and 60. So you'll get a 'd'. 95 大于 90,但也大于 80、70 和 60。所以你会得到一个“d”。
(And you're not handling 'd' in your switch.) (而且您没有在交换机中处理 'd'。)
I believe you want我相信你想要
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';
What you have does not mutually exclude any but the last two cases, 60 and above and lower.除了最后两种情况,60 及以上和以下,你所拥有的并不相互排斥。 Your code doesn't short circuit;
您的代码不会短路; it checks all of 1 through 5.
它检查所有 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';
I think you want to use 'else if'.我想你想使用'else if'。 It is falling down to the last if "score >= 60" which is true, and grade then equals "d", which produces the default case in your switch statement.
如果 "score >= 60" 为真,则它下降到最后一个,然后等级等于 "d",这会在您的 switch 语句中产生默认情况。
You have specified it such that your 95 satisfies all the cases: 95 is bigger than 90, but also bigger than 80, than 70, etc.您已指定它,以便您的 95 满足所有情况:95 大于 90,但也大于 80、大于 70 等。
In this case, the last one wins.在这种情况下,最后一个获胜。
You can solve it by either using else
s, or by wrapping it in a function and returning as soon as you know the grade you need:您可以通过使用
else
或将其包装在一个函数中并在知道您需要的等级后立即返回来解决它:
char grade(int score) {
if (score >= 90) return 'a';
if (score >= 80) return 'b';
...
}
The if branches are ordered wrong (or you need to provide else branches like so:)). if分支的顺序是错误的(或者您需要像这样提供else分支:))。
See it live here: http://ideone.com/2uSZT在这里看到它: 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;
}
It's because of your if statements up top.这是因为你的if语句在上面。 You should be using else if s instead of individual if s.
您应该使用else if s 而不是单独的if s。 Your if for 90 is following through, and then so are all the others.
你 90 的if正在跟进,然后所有其他人也是如此。 Your letter "a" is essentially being overwritten because 95 is >= to all of the other conditions.
您的字母“a”基本上被覆盖了,因为 95 >= 所有其他条件。 Using an else if will break the rest of the checks when a true one is found.
使用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';
Because all score
comparisons are not combined with if/else if
conditions.因为所有的
score
比较都没有结合if/else if
条件。 They are independent if
statements.它们是独立的
if
语句。 Thus grade
gets overwritten for 95
.因此
grade
被覆盖为95
。
You need to improve your if conditions.你需要改善你的if条件。
You are checking score >= no
.您正在检查
score >= no
。 The input 95
executes all the if statements and the last executed statement was the d
.输入
95
执行所有if语句,最后执行的语句是d
。 Now in your switch statement, case d
is not present so it executes the default
one.现在在您的switch语句中,
case d
不存在,因此它执行default
。
You've gotten some answers already, but I think I'll suggest a slightly different possibility that gets rid of most of the control flow and substitutes a bit of math:你已经得到了一些答案,但我想我会建议一种稍微不同的可能性,它摆脱了大部分控制流并替代了一些数学:
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];
}
So, we use score/10
to turn scores of 0-100 to 0-10.因此,我们使用
score/10
将 0-100 的分数变为 0-10。 We then look up the appropriate grade for a score, with f=0, d=1, c=2, b=3 and a=4.然后我们查找分数的适当等级,f=0、d=1、c=2、b=3 和 a=4。 We use that to select and print out the appropriate message.
我们使用它来选择并打印出适当的消息。 I've added messages (that may or may not be quite what you want) for the letters you skipped.
我为您跳过的信件添加了消息(可能是也可能不是您想要的)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.