如何在 else-if 语句中正确使用 or 语句 (||)

How to use the or statement (||) correctly, in an else-if statement

考虑这段代码:

string GameExit;
bool GameChoiceGo = true;

while (GameChoiceGo == true)
{
    system("cls"); 
    cout << "\n  Are you sure you want to exit? (Yes or No)     ";
    cin >>  GameExit;
    if (GameExit == "y" || "Y" || "yes" || "Yes" || "YES")
    {
        cout << "User typed Yes";
        Sleep(3000);
        system("cls");
        break;
    }
    if (GameExit == "n" || "N" || "no" || "No" || "NO")
    {
        cout << "User typed No";
        Sleep(3000);
        system("cls");
        GameChoiceGo = false;
    }
    else
    {
        cout << "\nI'm sorry but, " << GameExit << " is not a acceptable choice. Type: Yes or No.\n\n\n";
        Sleep(3000);
        system("cls");
    }
}
break;

这里只激活了第一条语句。即使用户键入 "No" 或其他任何内容,它也会输出 "user typed yes"

如果我只用一个语句(即 "y""n")替换 or statementselse-if statements 就会起作用。唯一的问题是,我想要用户可能在代码中键入的任何可能版本的 yes 和 no。

知道为什么代码无法正常工作吗?

GameExit == "y" || "Y" || ....

不正确。正确的方法是:

GameExit == "y" || GameExit == "Y" || ....

依此类推,无论是还是否。

在 "your" 代码中使用 OR 运算符的正确方法如下(注意在 || 运算符之间显式使用 == 语句):

    if (GameExit == "y" || GameExit =="Y" || GameExit =="yes" || GameExit =="Yes" || GameExit =="YES")
    {
        cout << "User typed Yes";
        Sleep(3000);
        system("cls");
        break;
    }
    if (GameExit == "n" || GameExit =="N" || GameExit =="no" || GameExit =="No" || GameExit =="NO")
    {
        cout << "User typed No";
        Sleep(3000);
        system("cls");
        GameChoiceGo = false;
    }

PS:以上答案并非旨在提供类似情况下的最佳编程实践,而是以最少的代码更改为 OP 提供具体答案:)

// -----

编辑:这是使用 STL 的更好方法。请注意,(未排序的)数组查找需要线性搜索,而 unordered_set 是一个哈希集,具有(平均)恒定时间查找。这会更快,尤其是当是、否等选项很多时。

    #include <unordered_set>

    ...
    // These sets can be as large as possible or even dynamically 
    //  updated while the program is running. insert, remove, lookup will
    // all be much faster than a simple array.
    unordered_set<string> ySet{"y", "Y", "yes", "Yes", "YES"};
    unordered_set<string> nSet{"n", "N", "no", "No", "NO"};

    if (ySet.find(GameExit) != ySet.end())
    {
        cout << "User typed Yes";
        Sleep(3000);
        system("cls");
        break;
    }
    if (nSet.find(GameExit) != nSet.end())
    {
        cout << "User typed No";
        Sleep(3000);
        system("cls");
        GameChoiceGo = false;
    }
    ...

抱歉,您必须为要检查的每个条件写 GameExit ==

if (GameExit == "y" || GameExit == "Y" || GameExit == "yes" || GameExit == "Yes" || GameExit == "YES")

如果你写 if ("y")(这基本上就是你正在做的,只是有更多的语句),const char[] 将衰减为 const char*,并且该指针将被比较至 0。现在,该指针将 永远不会 为空,因为总会为字符串文字分配内存。

更好的解决方案是 (1) 创建一个包含所有选项的数组,以便检查条件成为一个简单的搜索,或者 (2) 例如将输入转换为全小写,然后进行比较。

// 1)
std::vector<std::string> options = { "y", "Y", "yes", "Yes", "YES" };

if (std::find(options.begin(), options.end(), GameExit) != options.end());
// or
if (std::any_of(options.begin(), options.end(), [&GameExit](const auto& value) { 
        return GameExit == value;
   }); 

// 2)
std::transform(GameExit.begin(), GameExit.end(), GameExit.begin(), ::tolower);

if (GameExit == "y" || GameExit == "yes");

如果您不知道函数的作用,可以查找函数:)。

您需要像这样为每个表达式定义一个完整的等式:

if ( gameExit == "y" || gameExit == "Y" ) {}