0

我正在尝试学习 C++,到目前为止我一直是正确的,但我遇到了一个简单的问题,我对 case 语句感到困惑,我知道如何处理 if 语句,我非常理解它,我在我下面的代码中工作的类和方法完美地工作我想知道如何更改它if statement并将其替换为case statement,当我尝试它时它不起作用但这是我的 if 语句代码如果我如何使它工作想用case而不是if??提前谢谢

我的代码

void SetType(){
        cout<<"Book SetType"<<endl;
        Choice:
        cout<<"Please Select from the list: \n 1- Technical literature \n 2- Fiction literature \n 3- Textbook"<<endl;
        int i;
        cin >> i;
        if ((i>0)&(i<=3)) {
            if (i==1) Type="Technical literature";
            if (i==2) Type="Fiction literature";
            if (i==3) Type="Textbook";
        }
        else 
        {
            cout << "Erorr you entered a wrong choice" << endl;
            goto Choice;
        }

    }
4

4 回答 4

4
    switch(i) {
    case 1:
        Type="Technical literature";
        break;
    case 2:
        Type="Fiction literature";
        break;
    case 3:
        Type="Textbook";
        break;
    default:
        cout << "Erorr you entered a wrong choice" << endl;
        goto Choice;
    }
于 2012-11-16T22:08:09.307 回答
2

A switch/case完全适合您的情况。使用switch/case当您有离散值时,您可以对单个变量进行测试,在您的情况下i

它通过定义每个casedefault一个来工作,以防变量与任何情况不匹配。switch这是您的代码使用/的样子case

switch(i)
{
    case 1:
        Type="Technical literature";
        break;
    case 2:
        Type="Fiction literature";
        break;
    case 3:
        Type="Textbook";
        break;
    default: 
        cout << "Erorr you entered a wrong choice" << endl;
        goto Choice;
}

break用于防止代码从一case开始继续执行下面的代码case

我强烈建议您学习比使用 agoto回到您的选择选择更好的方法。

这是一个新版本,它的“输入循环”稍微好一点,没有使用goto

void SetType()
{
    cout << "Book SetType" << endl;
    bool validChoice;
    do
    {
        validChoice = true; // Invalidate it in case of wrong choice
        cout << "Please Select from the list: \n 1- Technical literature \n 2- Fiction literature \n 3- Textbook" << endl;
        int i;
        cin >> i;
        switch(i)
        {
        case 1:
            Type="Technical literature";
            break;
        case 2:
            Type="Fiction literature";
            break;
        case 3:
            Type="Textbook";
            break;
        default:
            cout << "Error you entered a wrong choice" << endl;
            validChoice = false;
            cin.clear();
            string dummyLine;
            getline(cin, dummyLine);
        }
    } while(validChoice == false);
}

我添加了一些代码来删除不是数字的输入,否则 cin 将继续失败。

于 2012-11-16T22:11:41.060 回答
0
void SetType(){
        cout<<"Book SetType"<<endl;
        Choice:
        cout<<"Please Select from the list: \n 1- Technical literature \n 2- Fiction literature \n 3- Textbook"<<endl;
        int i;
        cin >> i;
        switch(i)
        {
            case 1:
            {
                Type="Technical literature";
                break;
            }
            case 2:
            {
                Type="Fiction literature";
                break;
            }
            case 3:
            {
                Type="Textbook";
                break;
            }
            default:
            {
                cout << "Erorr you entered a wrong choice" << endl;
                goto Choice;
                break;
            }
        }

    }
于 2012-11-16T22:09:55.307 回答
0
void SetType(){
    cout<<"Book SetType"<<endl;
Choice:
    cout<<"Please Select from the list: \n 1- Technical literature \n 2- Fiction literature \n 3- Textbook"<<endl;
    int i;
    cin >> i;
    switch(i) {
        case 1: Type="Technical literature"; break;
        case 2: Type="Fiction literature"; break;
        case 3: Type="Textbook"; break;
        default:
            cout << "Erorr you entered a wrong choice" << endl;
            goto Choice;
    }
}

Ps 很多人一看到goto就出冷汗和心悸。

于 2012-11-16T22:11:44.827 回答