【问题标题】:How do I implement "inventory[0]" into If-Else Statements如何在 If-Else 语句中实现“inventory [0]”
【发布时间】:2017-06-10 15:12:44
【问题描述】:

所以我目前正在尝试为我的 C++ 类制作一个基于文本的游戏,但似乎无法弄清楚如何让 if-else 语句仅在有以前收集的项目以用于故事的情况下执行有意义。如果物品不在库存中,玩家的选项将再次循环,让他们选择不同的选择。我尝试这样做:else if(input6 == "rub" || inventory[0] == "Purple Fruit") 但它只会执行该语句,而不管玩家是否拥有该项目。谢谢你的时间!!我是认真的。

do {
    cout <<"You gotta start doing something quick before the swelling gets worse" <<endl;
    cout <<"Options are: \"rub\" the random substances oozing from your purple fruit onto your swelling neck, \"keep\" following the tracks until you could get help from the humans if those tracks actually belong to a human, or \"look\" around for anything to help cure your swelling neck\""<<endl;
    cin >>input6;

    if (input6 == "keep")
    {
        cout << "" <<endl;
        return 0;
    }

    else if (input6 == "look")
    {
        cout <<"Good luck" <<endl;
    }

    else if (input6 == "rub" || inventory[0] == "Purple Fruit")
    {
        cout << ".." <<endl;
        return 0;
    }

} while (input6 != "grab");

cout <<"Out"<<endl;
inventory[1] = "Pink Sap";
cout<< inventory[1] <<endl;

【问题讨论】:

标签: c++ if-statement


【解决方案1】:

你不能使用“=”来比较字符串。你需要使用strcmp函数来比较字符串。

【讨论】:

  • 问题标记为C++,不知道OP是用char数组还是std::strings。
  • 我确实可以。使用 == 比较字符串时执行程序没有问题
  • 我不确定您是如何比较代码中的字符串的。正如 Quentin 建议的那样,您最好在此处包含您的源代码,以便每个人都可以检查它
【解决方案2】:

else if (input6 == "rub" || inventory[0] == "Purple Fruit") 应该改为else if (input6 == "rub" &amp;&amp; inventory[0] == "Purple Fruit"),如果您需要“Purple Fruit”位于用户的第一个库存槽中以使用“rub”命令。如果用户的库存中也没有“Purple Fruit”,您应该添加另一个处理“rub”命令的案例。考虑这样的事情:

else if (input6 == "rub")
{
    if(inventory[0] == "Purple Fruit")
    {
        // user has item
    }
    else
    {
        // user does not have item
    }
}

另外,考虑使用某种“set”类,例如std::set,这将允许您拥有可以按名称查询的库存项目的无序集合,而不必担心哪个库存位置该项目占用。

例如,使用std::set::find,您可以像这样测试物品是否在您的库存中:

// assume inventory is std::set<std::string> and is already initalized and populated
if(inventory.find("Purple Fruit") != inventory.end())
{
    // item is in the inventory
}
else 
{
    // item is not in the inventory
}

【讨论】:

  • != inventory.end()) 代表什么?
  • 解释起来有点复杂,但是在标准库代码中有一个叫做迭代器的东西。如果你熟悉for 循环,比如for(int i = 0; i &lt; 5; i++),那么迭代器有点像变量istd::set 没有像数组那样的顺序 - 换句话说,set 中没有“第二项”的真正概念。为了“遍历”集合中的每个项目,您可以使用迭代器,例如 for(std::set&lt;std::string&gt;::iterator it = inventory.begin(); it != inventory.end(); ++it),在循环中您可以使用 (*it) 访问 std::string。 (下面的第 2 部分)
  • 当您使用std::set 时,可以非常快速方便​​地确定集合中是否存在给定值。这就是std::set::find 所做的。 find 返回的是一个迭代器,如果它返回 &lt;var name&gt;.end(),您知道它无法找到您正在寻找的东西,其中&lt;var name&gt; 是保存您的集合的变量。这是一个特殊的迭代器值,当您在循环中使用它时表示“不再有项目”,如上所述,它也可以作为一种说法“我没有找到你要求的东西”。这有意义吗?
【解决方案3】:

这并不像你认为的那样:

if (input6 == "keep")

假设input6 被定义为char 的数组并且足够大以容纳给定的输入,那么您所做的是比较数组input6 的地址(因为数组衰减为指向大多数情况下的第一个元素)和字符串常量"keep"的地址。这将始终评估为 false。

要比较字符串的内容,请使用strcmp 函数:

if (strcmp(input6, "keep") == 0)

如果两个操作数包含相同的以空字符结尾的字符串,则此函数返回 0。

【讨论】:

    猜你喜欢
    • 2013-03-05
    • 1970-01-01
    • 2022-11-16
    • 2019-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多