如何制作C++;程序循环? 我做了一个岩石、纸张、剪刀C++程序,但我很难让它循环。在这个程序中,它计算赢和输(平局不算),当它平局时,它会一直持续到赢/输。当赢/输发生时,变量(玩家赢的pWins或玩家输的ploss)都会增加。它询问用户是否想再次播放,如果用户说不,程序结束并显示分数
我的问题是如何让程序循环,以防用户选择继续游戏。我知道我应该写一些短文,但我不知道怎么写如何制作C++;程序循环? 我做了一个岩石、纸张、剪刀C++程序,但我很难让它循环。在这个程序中,它计算赢和输(平局不算),当它平局时,它会一直持续到赢/输。当赢/输发生时,变量(玩家赢的pWins或玩家输的ploss)都会增加。它询问用户是否想再次播放,如果用户说不,程序结束并显示分数,c++,loops,C++,Loops,我的问题是如何让程序循环,以防用户选择继续游戏。我知道我应该写一些短文,但我不知道怎么写 if( pInput == ROCK && cInput == ROCK ) { std::cout << "You tied with me!" << std::endl; } else if( pInput == ROCK && cInput == SCISSORS ) { std::cout << "Drats! Yo
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.";
}
if(pInput==ROCK&&cInput==ROCK)
{
std::coutFYI,您可以使用while
、for
和do 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;
}
}
bool can\u continue=true;
while(你能继续吗)
{
仅供参考,您可以使用while
、for
和do 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;
}
}
bool can\u continue=true;
while(你能继续吗)
{
仅供参考,您可以使用while
、for
和do 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;
}
}
bool can\u continue=true;
while(你能继续吗)
{
仅供参考,您可以使用while
、for
和do 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;
}
}
bool can\u continue=true;
while(你能继续吗)
{
cout您使用do while循环的想法是正确的。您可以尝试以下方法:
do {
//your program
} while(condition);
这样,您的程序将运行一次,然后在您的条件仍然为真的情况下继续运行。在您的情况下,条件将是某种形式的布尔型游戏。您考虑使用do while循环是正确的。您可以尝试以下操作:
do {
//your program
} while(condition);
这样,您的程序将运行一次,然后在您的条件仍然为真的情况下继续运行。在您的情况下,条件将是某种形式的布尔型游戏。您考虑使用do while循环是正确的。您可以尝试以下操作:
do {
//your program
} while(condition);
这样,您的程序将运行一次,然后在您的条件仍然为真的情况下继续运行。在您的情况下,条件将是某种形式的布尔型游戏。您考虑使用do while循环是正确的。您可以尝试以下操作:
do {
//your program
} while(condition);
这样,您的程序将运行一次,然后在您的条件仍然为真的情况下继续运行。在您的情况下,条件将是布尔上的某种形式的游戏。这可能是一般结构:
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();
}
这可能是总体结构:
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();
}
这可能是总体结构:
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();
}
这可能是总体结构:
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();
}
使用查找表和switch
语句将非常容易做到这一点。这两个选项可以组合成一个值,如state=pInput。在没有看到其余代码的情况下,很难提出有用的建议。设置pWins
和ploss
的位置在哪里?类似地,您是否有pTies代码>变量?似乎您需要一个pTied
变量(只是一个bool
)。任何时候结果是平局,将其设置为true;任何时候结果不是平局,将其设置为false。然后,您只需将整件事情包装在while
循环中。while(pTied){…}
。我同意@RSahu的观点-你展示的代码与你的问题无关!给我们展示一些需要循环的东西(如果你觉得if-else语句的大量悲惨列表需要循环,那么就假装它们都在一个函数中,这样我们就不会被那个可怕的怪物分心)。只需将放在while(true)中即可
围绕整个循环。在循环的底部,询问用户是否想再次播放。如果他们说不,请使用break
退出循环。使用查找表和switch
语句将非常容易。这两个选择可以组合成一个值,如state=pInput Hard to suggest在看不到其余代码的情况下做一些有用的事情。pWins
和ploss
在哪里设置?类似地,您是否有一个pTies
变量?似乎您需要一个pted
变量(只是一个bool
)。任何时候结果是平局,将其设置为true;任何时候结果不是平局,将其设置为false。然后,您只需将整个内容包装在while
循环中。while(pTied){…}
。我同意@RSahu-您显示的代码与您的问题无关!向我们展示一些需要在循环中的东西(如果你觉得if-else语句的巨大悲剧列表需要循环,那么就假装它都在一个函数中,这样我们就不会被那个可怕的怪物分心了。)只需将while(true)
围绕整个循环。在循环的底部,询问用户是否想再次播放。如果他们说不,请使用break
退出循环。使用查找表和switch
语句将非常容易。这两个选择可以组合成一个值,如state=pInput Hard to suggest在看不到其余代码的情况下做一些有用的事情。pWins
和ploss
在哪里设置?类似地,您是否有一个pTies
变量?似乎您需要一个pted
变量(只是一个bool
)。任何时候结果是平局,将其设置为true;任何时候结果不是平局,将其设置为false。然后,您只需将整个内容包装在while
循环中。while(pTied){…}
。我同意@RSahu-您显示的代码有n