如果仍然不满足要求,如何使 if 语句重复?
How do I make an if statement that repeats itself if the requirements are still not met?
这是我的代码。
#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
号码。
相关文章:
- 我的简单if-else语句是如何无法访问的代码
- Insert函数不适用于2 if语句C++
- If语句未被求值C++
- C++嵌套if语句,基本货币交换
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 是否可以使用if constexpr删除控制流语句
- 我似乎对if/else的基本语句有问题:/
- If语句在c++中被忽略
- 比较if语句中的数组值和int值
- 以在Qt中的IF语句中设置时间延迟
- Craps游戏问题,忽略if语句
- c++:定义if语句中的模板
- C++嵌套的 If 语句中,它无法按预期工作
- 如果中的多个语句是否与多个 if 相同?
- C++ If/Else 语句被跳过
- 使用语句if时逻辑的问题
- 如何添加语句if-else
- 如何将语句if(isalpha(c))从c++转换为MIPS ?
- 在if语句if C++中创建对象时出现作用域问题
- if语句-if/else和参数放置C++