1

好的,这是一个简单的代码示例:

    char answer;
    cin >> answer;
    switch(answer)
    {
        case 'y':
        case 'Y':
            inventory[0] = "Short Sword";
            cout << "\nYou place the Rusty Battle Axe in the chest.";
            break;

        case 'n':
        case 'N':
            inventory[0] = "Rusty Battle Axe";
            cout << "\nYou leave the Short Sword in the chest.";
            break;

        default :
            cout << "\nThat was an invalid response.";
    }

显然,我可以花一会儿时间(回答!='Y'||回答!=...)但是有没有更优雅的方法可以在执行默认案例后简单地返回第一个案例?因此,如果用户输入了错误的字母,我只需再次问他们问题,直到他们输入可接受的回复?

不,这不是家庭作业或任何东西。我正在阅读 Dawson 的 C++ Game Programming 一书,我想通过允许用户保留或交易物品来使程序示例更加生动。我把这一切都做得很好,但是如果输入了错误的响应,它只会显示库存的内容并退出。我想做到这一点。强制用户输入正确的响应,然后显示更新后的库存。

感谢帮助!

更新! 你们都给了我很多不同的方法——我真的很感激!我承认我可能没有正确设计这个 switch 语句,我为这个矛盾道歉。我会尝试你的每一个建议并在这里发回,选择一个作为答案。谢谢!

好的,我刚刚浏览了你所有的答案,并用我的代码尝试了其中的大部分。我选择了最简单、最优雅的解决方案作为我问题的答案。但是你们都帮助我看到了看待这个问题的不同方式,而且我现在对 switch 语句有了更多的了解。实际上,在我正在 YouTube 上由用户 What's A Creel 关注的教程中使用它来代替 while 循环

我真的很感谢你的帮助!我觉得我今天在编程实践中确实取得了很多成就。你们(和女孩)都很棒!

更新和完整的代码:

#include <iostream>
#include <string>

using namespace std;

// This program displays a hero's inventory

int main()
{
    const int MAX_ITEMS = 4;
    string inventory[MAX_ITEMS];

    int numItems = 0;
    inventory[numItems++] = "Rusty Battle Axe";
    inventory[numItems++] = "Leather Armor";
    inventory[numItems++] = "Wooden Shield";

    cout << "Inventory:\n";
    for(int i = 0; i < numItems; ++i)
    {
        cout << inventory[i] << endl;
    }

    cout << "\nYou open a chest and find a Lesser Healing Potion.";
    inventory[numItems++] = "Lesser Healing Potion";

    cout << "\nInventory\n";
    for(int i = 0; i < numItems; ++i)
    {
        cout << inventory[i] << endl;
    }

    cout << "\nYou also find a Short Sword.";
    if(numItems < MAX_ITEMS)
    {
        inventory[numItems++] = "Short Sword";
    }
    else
    {
        cout << "\nYou have too many items and can't carry another.";
        cout << "\nWould you like to trade the " << inventory[0] << " for the Short Sword? ";
    }

    while (true)
    {
        char answer;
        cin >> answer;
        switch(answer)
        {
            case 'y':
            case 'Y':
                inventory[0] = "Short Sword";
                cout << "\nYou place the Rusty Battle Axe in the chest." << endl;
                break;

            case 'n':
            case 'N':
                inventory[0] = "Rusty Battle Axe";
                cout << "\nYou leave the Short Sword in the chest." << endl;
                break;

            default:
                cout << "\nThat was an invalid response!";
                cout << "\nWould you like to trade the " << inventory[0] << " for the Short Sword? ";
                continue;
        }
        break;
    }

    cout << "\nInventory:\n";
    for(int i = 0; i < numItems; ++i)
    {
        cout << inventory[i] << endl;
    }
    return 0;
}
4

7 回答 7

2

好吧,添加一个循环,它会在你想要的任何地方“循环回来”。

请注意,整个正文switch只是一个带有标签的长语句。一旦您通过其中一个标签输入它,它就可以像任何其他语句一样工作。就像一个普通的 C++ 语句不会为你自己“回环”,除非你把它变成一个循环或使用goto,同样,它的主体也不会switch为你自己“回环”。

因此,如果您想将控制权转移回来 - 请使用适当的语言结构。您可以直接注入goto该语句的主体,它会照常工作。

switch(answer)
{
    case 'y':
    case 'Y':
    FIRST_OPTION:
        ...
        break;

    default :
        ...;
        goto FIRST_OPTION; // Jump to first option
}

您可能还想查看Duff 的设备,了解更复杂的控制转移内部switch语句示例。

但是,您的问题似乎自相矛盾。您声明如果答案无效,您想再次要求用户输入。但是用户输入是在switch. 那你为什么说要回到第一个选项switch???

于 2013-06-19T04:39:11.173 回答
2

continue您可以使用在最后中断并用于跳回顶部的一次性循环:

while(true)
{
    switch(...) {
        //...
        default:
            continue;
    }
    break;
};

也许更好的方法是定义一组有效字母,特别是如果您将在代码中的任何地方执行此类操作:

char GetChoice( const string & prompt, const string & valid_choices )
{
    while( cin.good() )
    {
        cout << prompt << " " << flush;

        char c;
        if( !cin.get(c) ) break;

        size_t pos = valid_choices.find(toupper(c));
        if( pos != string::npos ) return valid_choices[pos];
    }
    return 0;  // Error condition.
}

并像这样使用:

switch( GetChoice("Do you want cake?", "YN") )
{
    case 'Y':
        cout << "Cake for you.\n";
        break;
    case 'N':
        cout << "No cake for you.\n";
        break;
    case 0:
        exit(1);      // Error occurred
}
于 2013-06-19T04:44:18.993 回答
2
bool valid;
do
{
    char answer;
    cin >> answer;

    switch (answer)
    {
        case 'y':
        case 'Y':
            inventory[0] = "Short Sword";
            cout << "\nYou place the Rusty Battle Axe in the chest.";
            valid = true;
            break;

        case 'n':
        case 'N':
            inventory[0] = "Rusty Battle Axe";
            cout << "\nYou leave the Short Sword in the chest.";
            valid = true;
            break;

        default :
            cout << "\nThat was an invalid response.";
            valid = false;
            break;
    }
}
while (!valid);
于 2013-06-19T04:48:41.827 回答
1

在默认部分使用 goto 语句返回到输入部分

于 2013-06-19T04:40:29.283 回答
1

这是一种方法:

bool done = false;
while (!done) {
    char answer;
    cin >> answer;
    done = true;

    switch(answer)
    {
        case 'y':
        case 'Y':
            inventory[0] = "Short Sword";
            cout << "\nYou place the Rusty Battle Axe in the chest.";
            break;

        case 'n':
        case 'N':
            inventory[0] = "Rusty Battle Axe";
            cout << "\nYou leave the Short Sword in the chest.";
            break;

        default :
            cout << "\nThat was an invalid response.";
            done = false;
    }
}
于 2013-06-19T04:41:37.727 回答
1

使用whileordo while循环。

例如:

char answer;
bool loopback  = true;
do
{        
    cin >> answer;
    switch(answer)
    {
        case 'y':
        case 'Y':
            inventory[0] = "Short Sword";
            cout << "\nYou place the Rusty Battle Axe in the chest.";
            loopback  = false;
            break;

        case 'n':
        case 'N':
            inventory[0] = "Rusty Battle Axe";
            cout << "\nYou leave the Short Sword in the chest.";
            loopback  = false;
            break;

        default :
            cout << "\nThat was an invalid response.";
            loopback  = true;
    }
}
while (loopback);
于 2013-06-19T04:42:13.557 回答
0

你可以使用labelandgoto语句。标记您要求用户输入的语句并添加goto语句default以防万一。前任::

AskQuestion:
 cout << "Press 'Y' for Short Sword Or 'N' for Rusty Battle Axe" << endl;
char answer;
cin >> answer;
switch(answer)
{
    case 'y':
    case 'Y':
        inventory[0] = "Short Sword";
        cout << "\nYou place the Rusty Battle Axe in the chest.";
        break;

    case 'n':
    case 'N':
        inventory[0] = "Rusty Battle Axe";
        cout << "\nYou leave the Short Sword in the chest.";
        break;

    default :
        cout << "\nThat was an invalid response.";
        goto AskQuestion ;
}

另一种方法是使用do-while带条件的循环,while(answer != 'Y' || answer !=...)正如您已经在问题中评论的那样。前任::

do{
 cout << "Press 'Y' for Short Sword Or 'N' for Rusty Battle Axe" << endl;
 char answer;
 cin >> answer;
 switch(answer)
 {
    case 'y':
    case 'Y':
        inventory[0] = "Short Sword";
        cout << "\nYou place the Rusty Battle Axe in the chest.";
        break;

    case 'n':
    case 'N':
        inventory[0] = "Rusty Battle Axe";
        cout << "\nYou leave the Short Sword in the chest.";
        break;

    default :
        cout << "\nThat was an invalid response.";
 }
}while( answer != 'Y' || answer != 'y' || answer != 'N' || answer != 'n' ) ;
于 2013-06-19T04:43:51.603 回答