【问题标题】:Nested if Statement Problems - Comparison Operator嵌套 if 语句问题 - 比较运算符
【发布时间】:2017-01-03 05:45:50
【问题描述】:

(使用 dev c++)由于我对 c++ 很陌生,我决定制作一个基本的文本 RPG 游戏。我已经多次调试该程序,但它什么也没有出现。每次我测试程序时,假设我想攻击旅行者,它会打印 TEST(我暂时放在那里,直到我可以解决问题)。如果我将 if(attack=="talk"||"Talk") 与 if(attack=="attack"||"Attack") 切换,那么我想与旅行者交谈,它会打印“你冲上前去谋杀旅行者发现他没有武装!”所有帮助将不胜感激。

#include <iostream>
#include <string>


using namespace std;

int main() {
    string dir, attack, trade;
    string inventory[] = {"Food", "Sword", "Armor"};
    cout << "Do you go left, right or forward?" << endl;
    cin >> dir;
    if(dir=="left"||"Left") {
        cout << "You decide to go left" << endl;
        cout << "On the way you meet a traveller, do you attack or talk?" << endl;
        cin >> attack;
       if(attack=="talk"||"Talk") {
             cout << "TEST" << endl;
             }
       else if(attack=="attack"||"Attack") {
             cout << "You rush forward and murder the traveller to find he was un-armed!\a" << endl;
             }
       else  {
             cout << "test" << endl;
             }

     }
    else if(dir=="Right"||"Right") {
        cout << "You decide to go right" << endl;
        }
    else {
         cout << "You decide to go forward" << endl;
         }
    system("PAUSE");
    return 0;
}

【问题讨论】:

  • 将其更改为dir=="left"||dir=="Left"。否则它将永远是true
  • 在互联网上搜索“c++ 转换字符串”。如果在比较之前将字符串全部转换为大写或全部小写,则只需进行一次比较。
  • 顺便说一句,您在检查“正确”方向时有错字;两个比较文本相同。
  • 你的逻辑有缺陷。如果我在你的提示下输入“burrito”,它会报告我要继续,这是错误的。现在是开始考虑如何处理无效输入的好时机。

标签: c++ dev-c++


【解决方案1】:

您正在使用|| 运算符,例如if(dir=="Right"||"Right")

这是错误的用法,因为它总是将第二部分 "Right" 评估为 true。将您的比较ALL更改为此例程:

if(dir=="Right"|| dir=="Right")

但是,在这里,两个语句是相同的,因为“Right”与“Right”相同。检查你的逻辑,你的意思是dir == "Right" || dir == "right"吗?

在解决我提到的问题后,这是您的代码的干净副本:

int main() 
{
    string dir, attack, trade;
    string inventory[] = { "Food", "Sword", "Armor" };

    cout << "Do you go left, right or forward?" << endl;
    cin >> dir;

    if (dir == "left" || dir == "Left")
    {
        cout << "You decide to go left" << endl;
        cout << "On the way you meet a traveller, do you attack or talk?" << endl;
        cin >> attack;

        if (attack == "talk" || attack == "Talk")
            cout << "TEST" << endl;
        else if (attack == "attack" || attack == "Attack") 
                    cout << "You rush forward and murder the traveller to find he was un-armed!\a" << endl;
                else 
                    cout << "test" << endl;
    }
    else if (dir == "Right" || dir == "right") 
                cout << "You decide to go right" << endl;
            else 
                cout << "You decide to go forward" << endl;

    system("PAUSE");
    return 0;
}

【讨论】:

  • cin &gt;&gt; attack; if(attack=="talk"||"Talk") { cout &lt;&lt; "TEST" &lt;&lt; endl; } else if(attack=="attack"||"Attack") { cout &lt;&lt; "You rush forward and murder the traveller to find he was un-armed!\a" &lt;&lt; endl; } else { cout &lt;&lt; "test" &lt;&lt; endl; } 是主要问题,你认为你能帮忙吗?
  • @BramwellSimpson 我认为您需要再次阅读我的答案。看看你的if 声明:if(attack=="talk"||"Talk") .. 这是错误的
  • OP 可以通过在进行比较之前将文本转换全部小写或全部大写来消除此问题。
  • @ThomasMatthews 非常有用的评论。谢谢
  • @ThomasMatthews 感谢您的提示,现在就做。
【解决方案2】:

除了宋元瑶/FirstStep的回答外,只是一个解释:

if(dir == "left" || "Left")

等价于

if((dir == "left") || ("Left"))

又等价于

if((dir == "left") || ("Left" != 0))

而字符串文字“Left”在内存中的地址不等于 0...

旁注:“Left”甚至没有为此转换为 std::string。

【讨论】:

    猜你喜欢
    • 2022-01-09
    • 2014-10-17
    • 1970-01-01
    • 1970-01-01
    • 2012-05-30
    • 1970-01-01
    • 1970-01-01
    • 2011-03-08
    • 1970-01-01
    相关资源
    最近更新 更多