如何评估大于和小于同一while循环的数字?

时间:2016-10-06 23:56:31

标签: c++ loops

// 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循环将继续询问他们是否有效,直到他们输入一个,然后正常运行该程序。

3 个答案:

答案 0 :(得分:0)

首先,在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始终为true,第二个if始终为false。当用户写入无效输入时,您应该进入循环。当(userInput&lt; 1或userInput&gt; 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);

答案 1 :(得分:0)

据我所知,你正在寻找这样的东西:

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 

答案 2 :(得分:-1)

所以,如果这个人猜到太高或太低,你的状态会让你保持在while循环中。在while循环中,我将添加您想要重复的更新条件或语句。因此,在您的情况下,“您的猜测太高”或“您的猜测太低”并再次询问他们的输入。我不是专业人士,但我会通过构建2个while循环来保持简单,一个用于过高,一个用于过低,就像你的if语句一样。从字面上看,您可以将前两个if语句更改为while循环,并添加一些额外的cout行以要求此人再次猜测并验证其输入。我希望这有帮助。