text - C++ 中 if 和 else if 和 else 表达式中的文本冒险选项
问题描述
您好,我对 C++ 很陌生。
我正在制作一个测试文本冒险,但无法让冒险选择查看我的角色 Kyung 的选项。
我究竟做错了什么?
我正在尝试使用 else if 表达式使程序查看 Kyung,但是当我输入“TURN”以尝试查看 kyung 时程序结束。我不知道我做错了什么。
#include <iostream>
using namespace std;
int main()
{
string choice;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " |__________________________________________" << endl;
cout << " /..........................................." << endl;
cout << " /............................................" << endl;
cout << " /............................................." << endl;
cout << " /.............................................." << endl;
cout << " /..............................................." << endl;
cout << " /................................................" << endl;
cout << " /................................................." << endl;
cout << " /.................................................." << endl;
cout << " /..................................................." << endl;
cout << " /...................................................." << endl;
cout << " /....................................................." << endl;
cout << " /......................................................" << endl;
cout << " /......................................................." << endl;
cout << " /........................................................" << endl;
cout << " /........................................................." << endl;
cout << " /.........................................................." << endl;
cout << "/..........................................................." << endl;
cout << "You wake-up in a empty room, you are on the ground." << endl;
stupid1:
cout << "Choices: type STAND to stand." << endl;
cin >> choice;
if(choice == "STAND") {
cout << " you stood up." << endl;
stupid2:
cout << " | ---------------------------- " << endl;
cout << " | | ---------------------- | " << endl;
cout << " | | | _____ | | " << endl;
cout << " | | | / | | | " << endl;
cout << " | | | | | | | " << endl;
cout << " | | | \ / | | " << endl;
cout << " | | | __| |___ | | " << endl;
cout << " | | | / \ | | " << endl;
cout << " | | |___|____________|_____| | " << endl;
cout << " | |__________________________| " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " |_________________________________________________________" << endl;
cout << " /.........................................................." << endl;
cout << "/..........................................................." << endl;
cout << " You see a painting on the wall." << endl;
} else {
cout << "That is not a choice." << endl;
goto stupid1;
}
cout << "Choices, type TURN to turn around, or type INSPECT to inspect painting." << endl;
cin >> choice;
if(choice == "INSPECT") {
cout << " " << endl;
cout << " __________________________________________________ " << endl;
cout << " | | " << endl;
cout << " | | " << endl;
cout << " | | " << endl;
cout << " | | " << endl;
cout << " | _____________ | " << endl;
cout << " | / \ | " << endl;
cout << " | | | | " << endl;
cout << " | | __ __ | | " << endl;
cout << " | | O | O | | " << endl;
cout << " | | | | | " << endl;
cout << " | | |_ | | " << endl;
cout << " | | | | " << endl;
cout << " | \ ----- / | " << endl;
cout << " | _____| |_______ | " << endl;
cout << " | __/ \____ | " << endl;
cout << " | / \ | " << endl;
cout << " | / \ | " << endl;
cout << " | | | | " << endl;
cout << " | | | | " << endl;
cout << " |_____|______________________________________|_____| " << endl;
cout << " " << endl;
cout << " " << endl;
cout << "You inspected the painting." << endl;
stupid3:
cout << "Type BACK to go back." << endl;
cin >> choice;
if(choice == "BACK") {
cout << "You went back." << endl;
goto stupid2;
} else if(choice == "TURN"){
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " /-\/-\ | " << endl;
cout << " | | | | " << endl;
cout << " | | | | " << endl;
cout << " _____\ | /__ | " << endl;
cout << " / \ | " << endl;
cout << " | _____ _____ | | " << endl;
cout << " | W | | " << endl;
cout << " \____ __/ | " << endl;
cout << " / \ | " << endl;
cout << "__________________/_ _\____________________| " << endl;
cout << "....................| |.......................\ " << endl;
cout << "....................| |........................\ " << endl;
cout << "....................|_/-\_|.........................\ " << endl;
cout << ".....................................................\ " << endl;
cout << "......................................................\ " << endl;
cout << ".......................................................\ " << endl;
cout << "........................................................\ " << endl;
cout << ".........................................................\ " << endl;
cout << "..........................................................\ " << endl;
cout << "............................................................" << endl;
cout << "You saw Kyung" << endl;
} else {
cout << "That is not a choice." << endl;
goto stupid3;
}
}
return 0;
}
解决方案
这仅仅是因为您的TURN
选择在INSPECT
选择中:
if(choice == "INSPECT") {
...
if(choice == "BACK") {
cout << "You went back." << endl;
goto stupid2;
} else if(choice == "TURN"){
...
}
}
你应该将它TURN
从INSPECT
if 块中移出,如下所示:
if(choice == "INSPECT") {
...
}
...
if(choice == "BACK") {
cout << "You went back." << endl;
goto stupid2;
} else if(choice == "TURN"){
...
}
更多的:
你应该避免goto
在你的代码中使用 a,因为调试代码真的很困难goto
,请在Spaghetti code阅读更多内容。while
您可以通过为游戏循环和function
您的每个选择使用循环来重构您的代码。
这里有一个while
循环的工作版本:
#include <iostream>
using namespace std;
void startRoom()
{
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " |__________________________________________" << endl;
cout << " /..........................................." << endl;
cout << " /............................................" << endl;
cout << " /............................................." << endl;
cout << " /.............................................." << endl;
cout << " /..............................................." << endl;
cout << " /................................................" << endl;
cout << " /................................................." << endl;
cout << " /.................................................." << endl;
cout << " /..................................................." << endl;
cout << " /...................................................." << endl;
cout << " /....................................................." << endl;
cout << " /......................................................" << endl;
cout << " /......................................................." << endl;
cout << " /........................................................" << endl;
cout << " /........................................................." << endl;
cout << " /.........................................................." << endl;
cout << "/..........................................................." << endl;
cout << "You wake-up in a empty room, you are on the ground." << endl;
}
void standChoice()
{
cout << " you stood up." << endl;
cout << " | ---------------------------- " << endl;
cout << " | | ---------------------- | " << endl;
cout << " | | | _____ | | " << endl;
cout << " | | | / | | | " << endl;
cout << " | | | | | | | " << endl;
cout << " | | | \ / | | " << endl;
cout << " | | | __| |___ | | " << endl;
cout << " | | | / \ | | " << endl;
cout << " | | |___|____________|_____| | " << endl;
cout << " | |__________________________| " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " |_________________________________________________________" << endl;
cout << " /.........................................................." << endl;
cout << "/..........................................................." << endl;
cout << " You see a painting on the wall." << endl;
}
void notChoice()
{
cout << "That is not a choice." << endl;
}
void inspectChoice()
{
cout << " " << endl;
cout << " __________________________________________________ " << endl;
cout << " | | " << endl;
cout << " | | " << endl;
cout << " | | " << endl;
cout << " | | " << endl;
cout << " | _____________ | " << endl;
cout << " | / \ | " << endl;
cout << " | | | | " << endl;
cout << " | | __ __ | | " << endl;
cout << " | | O | O | | " << endl;
cout << " | | | | | " << endl;
cout << " | | |_ | | " << endl;
cout << " | | | | " << endl;
cout << " | \ ----- / | " << endl;
cout << " | _____| |_______ | " << endl;
cout << " | __/ \____ | " << endl;
cout << " | / \ | " << endl;
cout << " | / \ | " << endl;
cout << " | | | | " << endl;
cout << " | | | | " << endl;
cout << " |_____|______________________________________|_____| " << endl;
cout << " " << endl;
cout << " " << endl;
cout << "You inspected the painting." << endl;
}
void turnChoice()
{
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " /-\/-\ | " << endl;
cout << " | | | | " << endl;
cout << " | | | | " << endl;
cout << " _____\ | /__ | " << endl;
cout << " / \ | " << endl;
cout << " | _____ _____ | | " << endl;
cout << " | W | | " << endl;
cout << " \____ __/ | " << endl;
cout << " / \ | " << endl;
cout << "__________________/_ _\____________________| " << endl;
cout << "....................| |.......................\ " << endl;
cout << "....................| |........................\ " << endl;
cout << "....................|_/-\_|.........................\ " << endl;
cout << ".....................................................\ " << endl;
cout << "......................................................\ " << endl;
cout << ".......................................................\ " << endl;
cout << "........................................................\ " << endl;
cout << ".........................................................\ " << endl;
cout << "..........................................................\ " << endl;
cout << "............................................................" << endl;
cout << "You saw Kyung" << endl;
}
void gameLoop()
{
string choice;
startRoom();
cout << "Choices: type STAND to stand." << endl;
while(true)
{
cin >> choice;
if(choice == "STAND")
{
standChoice();
cout << "Choices, type TURN to turn around, or type INSPECT to inspect painting." << endl;
}
else if(choice == "INSPECT")
{
inspectChoice();
cout << "Type BACK to go back." << endl;
}
else if(choice == "TURN")
{
turnChoice();
cout << "Type BACK to go back." << endl;
}
else if(choice == "BACK")
{
cout << "You went back." << endl;
standChoice();
cout << "Choices, type TURN to turn around, or type INSPECT to inspect painting." << endl;
}
else if(choice == "EXIT")
{
break;
}
else
{
notChoice();
cout << "Choices: type STAND to stand." << endl;
}
}
}
int main()
{
gameLoop();
return 0;
}
推荐阅读
- java - 必须在 Esper 8 中使用 RMI 服务器才能运行时部署 Esper 工作流
- java - 如何在 Spring Data Solr 中关闭 SolrTemplate
- javascript - 尝试在 Google 表格中进行动态股权分割计算
- archer - Archer 中是否有一个函数可以在一个字段中绘制一条分隔两个输出的线?
- sql - 完全外连接类似功能但合并列
- php - 当我尝试回显每个索引时,并非所有数据都通过 mysqli_fetch_array() 函数保存在数组中
- c++ - 用于 VS2019 的 circa-2011 代码库的 FLTK 2.0 构建和演示
- react-native - 用户认证后导航到另一个屏幕
- gradle - Gradle C++ 依赖项可以针对不同的应用程序进行不同的设置吗?
- tensorflow-datasets - 如果 a 被打乱,tf.data.Dataset.zip(a, b) 会改变元素的顺序