简体   繁体   English

c ++ int switch语句

[英]c++ int switch statement

i have been working on a switch for integers for the last hour and a half now, i know how to do switch with char but this seems much hard for me.any advice will be appreciated.the problem i have is that i cant accept grades over 100 which this switch currently does 我一直在为整数开关工作过去一个半小时,我知道如何用char进行切换,但这对我来说似乎很难。任何建议都会受到赞赏。我遇到的问题是我不能接受成绩超过100这个开关目前做的

    int testScore;                     
    cout <<"Enter your test score and I will tell you \n";
    cout <<"the letter grade you earned ";
    cin >> testScore;

    switch(testScore/10)
{ 
    case 10:
    case 9:
        cout <<"Your grade is A.\n";
    break;
    case 8: 
        cout <<"Your grade is B.\n";
    break;
    case 7: 
        cout <<"Your grade is C.\n";
        break;
    case 6: 
            cout << "Your grade is D.\n";
        break;
    case 5: 
            cout << "Your grade is F.\n";
        break;

    default:
        cout << "That score isn’t valid\n";

    }

You're dividing by 10.0, which is a double and this will not compile. 你除以10.0,这是一个双倍,这将无法编译。 This has to be changed to 10. Also, you should precede the switch statement with an if statement that checks if it's in a valid range. 这必须更改为10.此外,您应该在switch语句之前添加一个if语句,以检查它是否在有效范围内。

#include<iostream>
#include<iomanip>
using namespace std;
int main() 
{

int testScore;
cout <<"Enter your test score and I will tell you \n";
cout <<"the letter grade you earned \n";
cin >> testScore;

if (testScore<=100 && testScore>=0)
    switch(testScore/10)
    {
        case 10:
        case 9:
            cout <<"Your grade is A.\n";
            break;
        case 8:
            cout <<"Your grade is B.\n";
            break;
        case 7:
            cout <<"Your grade is C.\n";
            break;
        case 6:
            cout << "Your grade is D.\n";
            break;
        case 5:
            cout << "Your grade is F.\n";
            break;

        default:
            cout << "That score isn’t valid\n";
    }
else
    cout <<"That score isn't valid\n";

return 0;
}

You really should do this with 'if' not a 'switch'. 你真的应该用'if'而不是'switch'来做到这一点。 Something like the following code (not tested): 类似下面的代码(未测试):

if (testScore >=0 && testScore <= 100)
{
    char grade;

    if (testScore >= 90)
        grade = 'A';
    else if (testScore >= 80)
        grade = 'B';
    else if (testScore >= 70)
        grade = 'C';
    else if (testScore >= 60)
        grade = 'D';
    else
        grade = 'F';

    cout << "Your grade is " << grade << endl;
}
else
{
    cout << "Score of " << testScore << " is not valid" << endl;
}

i just decided to do it this way, thanks for all your answers 我决定这样做,谢谢你的所有答案

int testScore;                     
    cout <<"Enter your test score and I will tell you \n";
    cout <<"the letter grade you earned ";
    cin >> testScore;

if (testScore >= 0 && testScore <=100)
{

        switch(testScore/10)
    { 
        case 10:
        case 9:
            cout <<"Your grade is A.\n";
        break;
        case 8: 
            cout <<"Your grade is B.\n";
        break;
        case 7: 
            cout <<"Your grade is C.\n";
            break;
        case 6: 
                cout << "Your grade is D.\n";
            break;
        default: 
                cout << "Your grade is F.\n";

        }

}

else 
    cout <<"That score isn't valid" << endl;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM