这是函数:如何退出循环,如果一个函数返回一个值
void LoseRollDice(int Result1, int Result2, int i)
{
if (i == 1 && (Result1 == 2 || Result1 == 3 || Result1 == 12))
{
cout << "You Lose." << endl;
return;
}
else if (i == 1 && (Result2 == 2 || Result2 == 3 || Result2 == 12))
{
cout << "Computer Loses." << endl;
return;
}
if (Result1 == 7 && i > 1)
{
cout << "You Lose." << endl;
return;
}
if (Result2 == 7 && i > 1)
{
cout << "Computer Loses." << endl;
return;
}
}
在这里,我用它:
for (int i = 1; i <= 5; i++)
{
cout << "Enter 1 to play, or 2 to exit: ";
cin >> chose;
cout << endl;
if (chose == 1)
{
ResultPlayer = RollDice();
ResultComputer = RollDice();
DisplayDice(ResultPlayer, ResultComputer, i);
cout << endl;
LoseRollDice(ResultPlayer, ResultComputer, i);
// if the player lose, how to break after this function if the condition is true in it??
WinRollDice(ResultPlayer, ResultComputer, i, SumPlayer, SumComputer);
}
else break;
}
如何摆脱for循环,如果条件功能是否正确? 如果我把循环就会爆发,即使条件不满足
你需要让函数返回一些东西,并根据返回的内容继续或结束循环。 – NathanOliver
我曾想过要像你说的那样做,但我认为这不是最好的休息方式! –