如何在同一个while循环中求大于和小于的数
How to evaluate number greater than and less than in the same while loop?
// DiceRollProject.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <time.h>
using namespace std;
int diceRoll(int max); // function definition
int getValidInteger();// function definition
int main() {
srand(time(0)); // seed the random number generator
int exitProgram = 0;
int guess, rollValue;
int maxRollValue = 6;
cout << "Hello! Let's play a dice game. Let me do the first roll for you.n" << endl;
rollValue = diceRoll(maxRollValue);
cout << "In this roll, you got: " << rollValue << "n" << endl;
do {
rollValue = diceRoll(maxRollValue);
cout << "What's your guess for the next roll? Enter an integer between 1 and " << maxRollValue << ": ";
guess = getValidInteger();
// TODO: Validate input
if (guess > rollValue)
{
cout << "The guess was too high!";
}
if (guess < rollValue)
{
cout << "The guess was too low!";
}
if (guess == rollValue)
{
cout << "You guessed correctly, congrats!";
}
cout << "In this roll, you got: " << rollValue << "n" << endl;
// TODO: Evaluate result
cout << "Enter 1 to exit or any other integer to continue rolling ";
exitProgram = getValidInteger();
cout << "n";
if (exitProgram == 1)
{
cout << "Sorry to see you go. Have a wonderful day!n" << endl;
}
} while (exitProgram != 1);
return 0;
}
// Roll the die
int diceRoll(int max) {
int rollValue;
rollValue = (rand() % max) + 1;
return rollValue;
}
// Check if user entered an integer
int getValidInteger() {
int userInput;
cin >> userInput;
while (userInput < 1) {
if (userInput < 1)
{
cout << "Please enter a number greater than or equal to 1n";
}
if (userInput > 6)
{
cout << "Please enter a number less than or equal to 6n";
}
}
if (cin.fail()) {
cin.clear();
cin.ignore();
cout << "Please enter an Integer only ";
cin >> userInput;
cout << "n";
}
return userInput;
}
我有一个骰子滚猜谜游戏,我试图评估用户的输入,以确保他们不能输入数量小于1大于6,不幸的是,只有我的if语句,他们仍然可以输入这些数字,尽管显示一个字符串,输入是无效的,我想做一个while循环,不断要求他们输入有效的数量等于或大于1,等于和小于6,如果用户继续输入一个错误的号码,while循环将继续要求他们输入一个有效的数字,直到他们输入一个,然后程序将正常运行。
首先,在while循环中有死代码。
while (userInput < 1) {
if (userInput < 1)
{
cout << "Please enter a number greater than or equal to 1n";
}
if (userInput > 6)
{
cout << "Please enter a number less than or equal to 6n";
}
}
在循环体中,第一个if总是为真,第二个总是为假。当用户写入无效输入时,应该进入循环。当(userInput <1或userInput> 6)
计算完while语句的条件后,应该要求用户输入
do {
cout << "Please enter an Integer only ";
cin >> userInput;
if (userInput < 1)
{
cout << "Please enter a number greater than or equal to 1n";
}
if (userInput > 6)
{
cout << "Please enter a number less than or equal to 6n";
}
}while(userInput < 1 || userInput > 6);
让你进入while循环的条件是,如果那个人猜得太高或太低。在while循环中,我将添加您想要重复的更新条件或语句。所以在你的情况下,"你的猜测太高了"或"你的猜测太低了",然后再次请求他们的输入。我不是专业人士,但我会通过构造两个while循环来保持简单,一个用于太高,一个用于太低,就像你的if语句一样。从字面上看,您可以将前两个if语句更改为while循环,并添加几行额外的cout来要求用户再次猜测并验证其输入。我希望这对你有所帮助。
根据我的理解,你正在寻找这样的东西:
int main (){
int my_magic_number=(rand()%6)+1,usernumber=-1;
bool state;
while (usernumber!=my_magic_number){
cin>>usernumber;
state = (usernumber<1||usernumber>6);
while (state) {
cout<<"You entered a number outside the range [1,6] please try againn";}
cin>>usernumber;
state = (usernumber<1||usernumber>6);
}
if (usernumber!=my_magic_number) {/* do whatever you want */}
} //while loop
} // main
相关文章:
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 大于65535的C++数组[size]引发不一致的溢出
- 查找最接近的大于当前数字的数字的索引
- 从值小于256的uint16到uint8的Endian安全转换
- 将stl字符串缩小到小于15个字符的容量
- 如何检查两个 std::向量在小于 O(n) 的时间复杂度内是否相等
- 如何找到大于整数的最小数字
- 如何在随机数列表中查找大于或小于的元素
- 创建一个循环,该循环将输出大于零且小于 60(不包括 60)的所有 5 的倍数
- 节点小于且大于特定节点的节点数
- 如何做比率和大于和小于C++
- 互锁。比较交换<Int> 使用大于或小于而不是相等
- 在C++中获取大于或等于一个双精度的最小浮点值(以及小于或等于一的最大浮点值)
- 如何返回小于或等于平均值的整数和大于平均值的整数
- 对字符串和基本数据类型应用大于或小于检查
- 是否有一种方法可以正确地比较一个float值是否大于/小于另一个
- 在 std::sort() 中的自定义函数中,>(大于)运算符与 <(小于运算符)的交换会导致分段错误吗?
- 大于和小于在一起
- 如何在同一个while循环中求大于和小于的数
- 如何直观地理解C++优先级队列容器比较器中的大于/小于运算符