gpt4 book ai didi

c++ - 我正在创建一个基于文本的冒险游戏,需要使用 GOTO 的替代方法

转载 作者:太空宇宙 更新时间:2023-11-04 13:46:09 25 4
gpt4 key购买 nike

我如何才能正确使用某种类型的 do、while 或 for 循环来防止用户输入答案“1”或“2”以外的任何内容?

如果他们不这样做,程序应该告诉他们他们不能那样做,然后将他们返回到上一个问题。

#include <iostream>
#include "Options.h"
#include <string>

using namespace std;

int main(int argc, char *argv[]) {
cout << "\tWelcome to my text adventure game\n" << endl;
cout << "Please enter your characters Name: ";
string name;
cin >> name;
cout << "\nWelcome " << name << endl;
cout << "\nPlease choose the side you would like to play in - ";
Options OptionsObject;
OptionsObject.optionsSide();

string answer;
while(answer != "quit") {
cin >> answer;
cout << answer << endl;
}

if ( answer == "1" ) {
cout << "You chose the good side. Let the game begin\n " << endl;
cout << "You are located in a city named after the warrior who saved it from the evil\nmany years ago. The city of Redshore. " << endl;
cout << "You are no ordinary man in the City of Redshore. You are the king who rules it\nYou are seen as King of Justice, a good king.\nOne who only want what is best for his people" << endl;
cout << "but also a troubled man. You experienced something traumatizing when you\nwere a just a little boy, but no one knows about it,\nno one but yourself that is " << endl;
} else if( answer == "2" ) {
cout << "hey there" << endl;
}
}

最佳答案

看到这个问题会出现在用户在游戏过程中会做的所有选择中,我会做一个外部函数,让用户选择。也许静态类以这种方式工作。

int ans = Answer.getUserAnswer(2,"[1/2]>");

这应该以这种方式工作:

public static int getUserAnswer(int max =1,string message="[1/1]>")
{
int ans = 0;
while(ans==0){
cout<<message<<" ";
cin >> answer;
if(answer>0 and answer<=max) return ans;
cout<<"\tnot a valid choose\n";
ans=0;
}
}

并且您将在 ans 中拥有您期望的值之一,如果答案不在您期望的值之间,它将打印“无效选择”,即使有 10 个答案,您也可以这样做,称之为默认情况下,您只希望给他第一名。

我在自己制作的主机游戏中使用了它 ;)希望有用

编辑

int getUserAnswer(int max =1,string message="[1/1]>")
{
int ans = 0;
while(ans==0){
cout<<message<<" ";
cin >> answer;
if(answer>0 and answer<=max) return ans;
cout<<"\tnot a valid choose\n";
ans=0;
}
}

并在您的代码中请求此代码:

cout  << "\nWelcome " << name << endl;
cout << "\nPlease choose the side you would like to play in - ";
Options OptionsObject;
OptionsObject.optionsSide();

int answer =getUserAnswer(2,"[1/2]>");

if (answer == 1){

cout << "You chose the good side. Let the game begin\n " << endl; cout << "You are located in a city named after the warrior who saved it from the evil\nmany years ago. The city of Redshore. " << endl; cout << "You are no ordinary man in the City of Redshore. You are the king who rules it\nYou are seen as King of Justice, a good king.\nOne who only want what is best for his people" << endl; cout << "but also a troubled man. You experienced something traumatizing when you\nwere a just a little boy, but no one knows about it,\nno one but yourself that is " << endl;
}

else if(answer == 2){

cout << "hey there" << endl;

}

关于c++ - 我正在创建一个基于文本的冒险游戏,需要使用 GOTO 的替代方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25873366/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com