【问题标题】:How do I make parts of a C++ program loop?如何制作 C++ 程序循环的一部分?
【发布时间】:2015-09-14 17:25:46
【问题描述】:

我制作了一个石头剪刀布的 C++ 程序,但我很难让它循环。在程序中,它计算输赢(不计算平局),当它平局时,它会一直持续到赢/输。当发生赢/输时,任一变量(玩家赢的 pWins 或玩家输的 pLosses)都会增加。它询问用户是否想再玩一次,如果用户说不,程序结束并显示分数。

我的问题是如何让程序在平局和用户选择继续游戏的情况下循环。我知道我应该放一些do-whiles,但我不知道怎么写。

if( pInput == ROCK && cInput == ROCK )
{
    std::cout << "You tied with me!" << std::endl;
}
else if( pInput == ROCK && cInput == SCISSORS )
{
    std::cout << "Drats! You win!" << std::endl;
}
else if( pInput == ROCK && cInput == PAPER )
{
    std::cout << "Hah! Here comes the Hug of Death!" << std::endl;
}
else if( pInput == SCISSORS && cInput == SCISSORS )
{
    std::cout << "Looks like we tied!" << std::endl;
}
else if( pInput == SCISSORS && cInput == ROCK )
{
    std::cout << "Hah! I smashed you, so I win!" << std::endl;
}
else if( pInput == SCISSORS && cInput == PAPER )
{
    std::cout << "Drats! You win!" << std::endl;
}
else if( pInput == PAPER && cInput == PAPER )
{
    std::cout << "Drats! We tied!" << std::endl;
}
else if( pInput == PAPER && cInput == SCISSORS )
{
    std::cout << "Hah! I win, because I'm a scissor!" << std::endl;
}
else if( pInput == PAPER && cInput == ROCK )
{
    std::cout << "Drats! You gave me the Hug of Death!" << std::endl;
}

std::cout << "You won " << pWins << " times, and lost " << pLosses << "times.";

}

【问题讨论】:

  • 使用查找表和switch 语句会容易得多。这两个选择可以组合成一个值,例如state = pInput &lt;&lt; 2 | cInput。然后,您可以使用单数 switch 测试与所有合理结果对所有可能的匹配项进行测试。至于循环,你用过forwhile吗?
  • 很难在没有看到其余代码的情况下提出有用的建议。
  • pWinspLosses 设置在哪里?同样,您有 pTies 变量吗?看起来你想要一个pTied 变量(只是一个bool)。任何时候结果为平局,将其设置为 true;任何时候结果不是平局,将其设置为假。然后,您只需将整个内容包装在 while 循环中。 while (pTied) { ... }.
  • 我同意@RSahu - 您显示的代码与您的问题无关!向我们展示一些需要循环的东西(如果你觉得 if-else 语句的巨大悲惨列表需要循环,假装它们都在一个函数中,这样我们就不会被那个可怕的怪物分心) .
  • 只需将while (true) 放在整个事情周围。在循环的底部,询问用户是否想再次播放。如果他们说不,请使用break 退出循环。

标签: c++ loops


【解决方案1】:

仅供参考,您可以使用whilefordo-while 在循环中进行迭代。

您必须确定循环的“退出”标准。

这是一个例子:

bool can_continue = true;
while (can_continue)
{
  cout << "Do you want to continue?";
  std::string answer;
  getline(cin, answer);
  if (answer = "no")
  {
    can_continue = false;
  }
}

编辑 1:
您可以通过检查关系来简化程序:

if (pInput == cInput)
{
    std::cout << "You tied with me!" << std::endl;
}
else
{
  // Check for winning combinations.
}

【讨论】:

    【解决方案2】:

    您考虑使用 do while 循环是正确的。你可以试试这样的:

    do {
        //your program
    } while(condition);
    

    这样,您的程序将运行一次,然后在您的条件仍然成立时继续运行。您的情况是某种形式的布尔游戏。

    【讨论】:

      【解决方案3】:

      这可能是一般结构:

      int main() {
          do {
              pInput = getPlayerInput();
              cInput = getComputerInput();
              result = checkResult(pInput, cInput); // here you could put your code, and you 
                                                    // could also set pWins and pLosses here
      
              if (result == tie) {
                  playAgain = true;
              } else {
                  playAgain = askPlayAgain();
              }
          } while (playAgain);
      
          print_results();
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-12-17
        • 2020-05-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-29
        • 2019-01-24
        相关资源
        最近更新 更多