C++如何重新启动if语句?
假设您的代码是:C++如何重新启动if语句?,c++,loops,if-statement,C++,Loops,If Statement,假设您的代码是: cout << "You wake up in a room. There is a small lit candle and a door. What do you do?" << endl; string startout; getline(cin, startout); if (startout == "Door" || startout == "DOOR" || startout == "d
cout << "You wake up in a room. There is a small lit candle and a door. What do you do?" << endl;
string startout;
getline(cin, startout);
if (startout == "Door" || startout == "DOOR" || startout == "door" || startout == "Open Door" || startout == "open door" || startout == "OPEN DOOR" || startout == "Open door" || startout == "Open the door" || startout == "open the door") {
cout << " " << endl;
cout << "You move to the door but its too dark to see anything besides the outline of it." << endl;
} else if (startout == "Candle" || startout == "CANDLE" || startout == "candle" || startout == "Pick Up Candle" || startout == "PICK UP CANDLE" || startout == "pick up candle" || startout == "Pick up candle") {
cout << "You pick up the candle then move to the door. With the light from the candle you can see the door well. What do you do?" << endl;
};
如果使用代码的人键入了除Door、Candle或任何变体之外的其他内容,那么重新启动If语句以重新询问第一个问题的代码是什么
例:用户输入:舞蹈
输出:我不懂跳舞。
你是做什么的
或者类似的东西 您可以在外部添加whiletrue循环,并添加if条件,该条件在正确输入时中断
另外,在getline命令之后,在else if条件之后添加else条件:
else
cout << "I don't understand " << startout << ". What do you do?" << endl;
在其他if和else if条款中,添加中断;命令。要运行直到他们键入正确的内容吗?这很简单。你应该使用一个循环 循环通常是一个语句块,在满足某个条件之前它会重复自身。例如,一个好的循环是: 重复该程序,直到字符串等于门或蜡烛 这种情况,我看你已经比较熟悉了。然后让我向您介绍while循环块的语法,我认为它最适合您的案例,而while是另一个适合您的循环,但解释起来稍微复杂一些。 编辑:我正在用rain的评论更改代码:
string startout=""; // Initiallizing it before the while loop, so it could be used inside it.
cout << "You wake up in a room. There is a small lit candle and a door. What do you do?" << endl;
while(startout !="door" && startout !="candle") // Do the following actions in this block until startout equals "Door" or "Candle".
{ // start of a while block
getline(cin, startout);
if (startout.tolower().find("door")!=npos) // If the string contains any variation of door:
{
cout << " " << endl;
cout << "You move to the door but its too dark to see anything besides the outline of it." << endl;
}
else if (startout.tolower().find("candle")!=npos) // same, with candle. Much more elegant, like thomas said.
{
cout << "You pick up the candle then move to the door. With the light from the candle you can see the door well. What do you do?" << endl;
}
else
{
cout << "I don't understand " << startout << ". What do you do?" << endl; // If you didn't get what you want, ask for another string and restart.
}
} // end of the while block.
这不是最优雅的方法,但你知道了。首先,你需要一个更好的解析器。至少,把 表或某种映射中的合法值,带有指向 行动然后将输入包装到函数中:
typedef void (* ActionPointer)(); // Or whatever you need.
typedef std::map< std::string, ActionPointer, CaseInsensitiveCmp >
ActionMap;
ActionPointer
getAction( ActionMap const& legalValues )
{
std::string line;
if ( ! std::getline( std::cin ) ) {
// Error on std::cin... Probably fatal.
}
ActionMap::const_iterator action = legalValues.find();
return action == legalValues.end()
? nullptr
: action->second;
}
然后你可以写一些类似的东西:
std::cout << "You wake up in a room. There is a small lit candle and a door."
" What do you do?" << std::endl;
ActionPointer nextAction = getAction();
while ( nextAction == nullptr ) {
std::cout << "I don't understand. What do you do?" << std::endl;
nextAction = getAction();
}
(*nextAction)();
如果您想在错误消息中获得更多信息,可以安排
返回一个带有指针的结构,该信息仍在测试中
指针。所以。。。你想要一个循环,我是在Reddit上帮助你的人——一旦你开始将它应用到你树的每一根树枝上,它就会变得非常丑陋。你真的需要开始研究使用有限状态机,而不是一个很深的if语句树。你还应该把你的输入转换成小写,然后你只需要比较startout==door | | startout==opendoor,而不是所有的大写组合。@JosephMansfield或其他什么。一长串if肯定不是解析输入的好方法。我在回答中没有说得那么远,但是如果我接受一整行的输入,我会从消除噪音开始,比如,然后以其他方式规范输入。@JosephMansfield开始,我读到资本主义的每一个组合:使用无止境的循环比不优雅稍微多一些-他几乎给了你这个词。@a.Abramov哦,来吧。从外观上看,这是一个基于命令行的RPG。整个游戏是一个无限循环,在某个游戏结束时中断。拥有这个不定式循环不会造成更多的伤害。我的观点是,它是一个RPG。因此,他可能想在循环完成后做一些事情。您的解决方案没有错-但是,它也不对。@user3109672您总是可以用一个带条件的普通循环重写一个无限循环,并在其中插入一些断点-这样做更简洁。@JosephMansfield我支持这一点。更容易理解和分析所有条件都在顶部的普通循环。您可以通过将文本转换为所有小写或所有大写来减少if语句。这将把if语句中的表达式数量减少到1。@ThomasMatthews刚刚做了,只是增加了一点:好主意。从长远来看,他可能会想要更多不同的操作,我会使用一个映射,而不是一系列if。我肯定会把实际行动放在循环之外。看起来像一个解释器。。。。相当整洁;然而,我不确定是否所有的指针和地图都适合一个还没有学会循环的人。…@a.Abramov地图对他来说可能是新的,但他肯定应该使用它们,而不是笨重的if。如果不习惯,指向函数的指针可能有点棘手。这是我会使用的解决方案,但您也可以轻松地用枚举和开关替换它。然而,最重要的一点是,这至少需要在两个不同的功能中。嗯,你说服了我。虽然我必须提到ifs很容易更改,但只要这两个命令,就可以使它们看起来更漂亮。@A.Abramov这也是代码行的问题。毫无疑问,他的代码将会增长,当您开始考虑5或10条命令时,if开始变得非常笨拙。如果我写这篇文章,我会将循环处理错误也放在一个单独的函数中,这样我就可以在许多不同的上下文中使用不同的操作列表来重用它。但是,当然,这样做,而不是一个指向函数的指针,您所需要的只是下一条消息和指向下一个映射的指针。说得好,我的想法是:
std::cout << "You wake up in a room. There is a small lit candle and a door."
" What do you do?" << std::endl;
ActionPointer nextAction = getAction();
while ( nextAction == nullptr ) {
std::cout << "I don't understand. What do you do?" << std::endl;
nextAction = getAction();
}
(*nextAction)();