我如何获取代码的一部分来循环并重复一个问题,直到答案是有效的输入C++
代码运行良好,我只是缺少了一个元素,如果结尾要求重复游戏,如果我以用户身份输入,我希望代码用一个新的cout语句重复问题,例如无效答案,请回答是/否,如果是,显然游戏会重新启动 石头剪刀布游戏b/w 2玩家我如何获取代码的一部分来循环并重复一个问题,直到答案是有效的输入C++,c++,loops,while-loop,do-while,C++,Loops,While Loop,Do While,代码运行良好,我只是缺少了一个元素,如果结尾要求重复游戏,如果我以用户身份输入,我希望代码用一个新的cout语句重复问题,例如无效答案,请回答是/否,如果是,显然游戏会重新启动 石头剪刀布游戏b/w 2玩家 int main(int argc, const char * argv[]) { char playAgain ='y' ; // loop control do { char Player1; char Player2 = '\0'; cout <
int main(int argc, const char * argv[]) {
char playAgain ='y' ; // loop control
do
{
char Player1;
char Player2 = '\0';
cout << "Player 1, Enter R, P, or S: "; // Player 1
cin >> Player1;
Player1 = toupper(Player1);
while (Player1 != 'R' && Player1 != 'P' && Player1 !='S' )
{
cout << "please only answer R , P , or S: " << endl;
cin >> Player1;
Player1 = toupper(Player1);
}
{
cout << "Player 2, Enter R, P, or S: "; // Player 2
cin >> Player2;
Player2 = toupper(Player2);
while (Player2 != 'R' && Player2 != 'P' && Player2 !='S' )
{
cout << "please only answer R , P , or S: " << endl;
cin >> Player2;
Player2 = toupper(Player2);
}}
if (Player1 == Player2) //TIE
{
cout << "Nobody wins."<<endl;}
else if (Player1 == 'R' && Player2 == 'P')
{
cout << "Paper covers rock, Player 2 wins."<< endl;
}
else if (Player1 == 'P' && Player2 == 'R')
{
cout << "Paper covers rock, Player 1 wins."<< endl;
}
else if (Player1 == 'S' && Player2 == 'P')
{
cout << "Scissors cut paper, Player 1 wins."<< endl;
}
else if (Player1 == 'P' && Player2 == 'S')
{
cout << "Scissors cut paper, Player 2 wins."<< endl;
}
else if (Player1 == 'R' && Player2 == 'S')
{
cout << "Rock breaks scissors, Player 1 wins."<< endl;
}
else if (Player1 == 'S' && Player2 == 'R')
{
cout << "Rock breaks scissors, Player 2 wins."<< endl;
}
{ cout << "Play again? (y/n): "; // Player 1
cin >> playAgain;
if (playAgain=='N' || playAgain=='n')
{ cout <<"BYEEEEE"<<endl;}
}}
while (playAgain=='Y' || playAgain=='y');
return 0;
}
我认为最优雅的方法是:
#include <iostream>
#include <string>
#include <set>
static const std::set<std::string> RPS {
"R",
"P",
"S"
};
static const std::set<std::string> yesno {
"yes",
"no"
};
std::string ask_user(
const std::string& question,
const std::set<std::string>& valid_answers
) {
std::cout << question << std::flush; // outputs question (and flushes cout so it displays)
std::string answer;
while (true) { // this loop will terminate only when "break;" is reached
std::getline(std::cin, answer); // get answer
if (valid_answers.count(answer) == 0) { // if answer is not in valid_answers
std::cout << "Invalid answer!" << std::endl; // complain to the user
} else { // if answer is not invalid
break; // exit loop
}
}
return answer;
}
或者对于这样的是/否回答:
std::string p1_move = ask_user("Player 1 move (R/P/S)?\n", RPS);
std::string answer = ask_user("Another game (yes/no)?\n", yesno);
int main() {
while(true) {
char input;
std::cout << "Would you like to continue the game? (y/n): ";
std::cin >> input;
if(input == 'n' || input == 'N')
return 0;
else if(input == 'y' || input == 'Y') {
startGame();
break;
} else {
std::cout << "Invalid response." << std::endl;
}
}
}
这将防止您在请求用户移动和请求用户进行另一个游戏时重复代码,因此这是更好的代码练习。您可以执行以下操作:
std::string p1_move = ask_user("Player 1 move (R/P/S)?\n", RPS);
std::string answer = ask_user("Another game (yes/no)?\n", yesno);
int main() {
while(true) {
char input;
std::cout << "Would you like to continue the game? (y/n): ";
std::cin >> input;
if(input == 'n' || input == 'N')
return 0;
else if(input == 'y' || input == 'Y') {
startGame();
break;
} else {
std::cout << "Invalid response." << std::endl;
}
}
}
^明白了!是的,回到我的缩进,和我的第一个循环相比。非常感谢大家的帮助:作为一个友好的建议,我强烈建议您在代码中保持一致的缩进,即使从技术上讲,它不需要运行。它使代码更易于阅读,无论是对您自己还是对他人。一旦你开始做更大的项目,你会发现修改代码和理解你写的东西要容易得多。你已经为R、P或s输入执行了输入验证。验证下一个输入有什么不同?顺便说一下,另一种风格建议是命名变量,以便名称反映其内容或用途。Player1对玩家来说是个好名字,例如一个拥有名字和分数的结构,但对输入来说是个坏名字。更好的名称可以是Player1Input;-。另一个建议是查找do…while循环,当操作(例如用户输入)总是需要时,可以使用该循环来避免代码重复,并且应该重复,直到满足条件。非常感谢您,我仍然在学习代码,因此出现了平民错误:这是一个很好的解决方案。可以从集合中构造问题中的选项列表,该列表具有可维护性优势,即当集合稍后更改时,问题会自动更新,但这可能是设计过度了。不知道