如果仍然不满足要求,如何使 if 语句重复?

How do I make an if statement that repeats itself if the requirements are still not met?

本文关键字:语句 if 何使 满足要求 如果      更新时间:2023-10-16

这是我的代码。

#include <iostream>
using namespace std;
int main()
{
int i = 0;
int players;
int silenceAmount;
int bleedAmount;
int bleedDays;
int playersAlive;
int playersDead;
int playersAllowed = 6;
cout << "How many players are playing (can only be " << playersAllowed << ")? ";
cin >> players;
if (players > playersAllowed) (
cout << "There an only be " << playersAllowed << " players. Please select another number. ");
cin >> players;
cout << "There are " << players << " players.nn";

return 0;
}

这只工作一次,我希望它工作,直到它得到一个小于或等于 6 的数字。

重复if语句通常作为while循环完成。在您的代码中,当您有

if (players > playersAllowed)

你应该简单地改变它做

while (players > playersAllowed)

另外,当我这样做时,您对if语句的语法对于您要执行的操作不正确。您应该分别将()替换为{}。此外,结束括号位于不正确的位置。

最后,你的循环将是这样的:

while (players > playersAllowed) {
cout << "There can only be " << playersAllowed << " players. Please select another number: ";
cin >> players;
}

请注意,这不会考虑某人输入jfksdjfs号码。