如何在同一个while循环中求大于和小于的数

How to evaluate number greater than and less than in the same while loop?

本文关键字:大于 小于 同一个 while 循环      更新时间:2023-10-16
// 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