如何使我的代码的一部分循环并重复一个问题,直到答案是有效的输入C ++

时间:2019-01-08 02:50:35

标签: c++ loops while-loop do-while

代码可以正常工作,我只是缺少以下元素:如果以用户身份输入“ a”作为重复游戏的结束要求,我希望代码以新的cout语句(例如“请回答无效”)重复该问题回答是/否,如果是,显然游戏会自动重启 (ROCK剪刀游戏,黑白2名玩家)

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;

}

3 个答案:

答案 0 :(得分:1)

我相信最优雅的方法是这样做:

df[1, 5]
# Error in df[1, 5] : object of type 'closure' is not subsettable

然后您可以要求采取以下行动:

df

或回答是/否:

#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;
}

这将避免您不得不重复代码来询问用户一个动作以及询问用户另一个游戏,因此这是更好的代码实践。

答案 1 :(得分:0)

您可以执行以下操作:

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;
        }
    }
}

答案 2 :(得分:0)

cout << "Play again? (y/n): ";
    cin >> playAgain;
while (playAgain != 'Y' && playAgain != 'y' && playAgain !='n' )
{     cout << "Error input :Play again? (y/n): ";         // Player 1
    cin >> playAgain;

^想通了!是的,回到我的缩进,并与我的第一个循环进行比较。非常感谢大家的帮助:)