繁体   English   中英

如何在同一个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 1\n";
        }

        if (userInput > 6)
        {
            cout << "Please enter a number less than or equal to 6\n";
        }

    }


    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 循环将不断要求他们输入一个有效数字,直到他们输入 1,然后该程序将照常运行。

首先,在 while 循环中,您有死代码。

while (userInput < 1)  {

    if (userInput < 1)
    {
        cout << "Please enter a number greater than or equal to 1\n";
    }

    if (userInput > 6)
    {
        cout << "Please enter a number less than or equal to 6\n";
    }

}

在循环体中,第一个 if 始终为真,第二个 if 始终为假。 当用户写入无效输入时,您应该进入循环。 这发生在 (userInput < 1 or 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 1\n";
    }

    if (userInput > 6)
    {
        cout << "Please enter a number less than or equal to 6\n";
    }

}while(userInput < 1 || userInput > 6);

因此,让您保持在 while 循环中的条件是该人的猜测太高或太低。 在 while 循环中,我将添加您想要重复的更新条件或语句。 因此,在您的情况下,“您的猜测太高”或“您的猜测太低”并再次询问他们的意见。 我不是专业人士,但我会通过构建 2 个 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 again\n";}
        cin>>usernumber;
        state = (usernumber<1||usernumber>6);
     }
    if (usernumber!=my_magic_number) {/* do whatever you want */}
} //while loop
} // main 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM