C++中的Switch语句

Switch statement in C++

本文关键字:语句 Switch 中的 C++      更新时间:2023-10-16

考虑:

#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。我们使用它来选择并打印出合适的消息。我为你跳过的信件添加了信息(可能不是你想要的(。