C++:提示需要多次输入数据才能继续

C++: Prompt Requires Data to be Inputted Multiple Times to Proceed

我编写的以下程序提示用户输入一个数字 1 - 100。该程序从随机数生成器中预选了一个数字,用户必须猜测该数字。如果用户的猜测太高或太低,那么程序将通知用户,直到用户的猜测是正确的。我的程序运行良好,但是当提示输入数字时,在某些情况下我必须输入数字四次或更多次,程序才会提示我。有人可以告诉我或帮助我找出我的错误吗?

#include <iostream>
#include <ctime>//A user stated that using this piece of code would make a true randomization process.
#include <cstdlib>
using namespace std;

int main()//When inputing a number, sometimes the user has to input it more than once.
{
    int x;
    int ranNum;
    srand( time(0));

    ranNum = rand() % 100 + 1;

    cout << "Please input your guess for a random number between 1 - 100.\n";
    cin >> x;

    while (x > ranNum || x < ranNum)
    {
        {
            if (x > ranNum)
                cout << "Your input was greater than the system's generated random number. Please try again.\n";
            cin >> x;
            if (x > 100 || x < 1)
                cout << "Input invalid. Please input a number between 1 - 100.\n";
            cin >> x;
        }
        {
            if (x < ranNum)
                cout << "Your input was less than the system's generated random number. Please try again.\n";
            cin >> x;
            if (x > 100 || x < 1)
                cout << "Input invalid. Please input a number between 1 - 100.\n";
            cin >> x;
        }
        {
            if (x == ranNum)
                cout << "You guessed right!\n";
        }
    }

    return 0;
}

你的括号放错地方了。包含多行的 if 语句应采用

的形式
if (condition)
{
    code1
    code2
    code3
    ...
}

你拥有的是

{
    if condition
        code1
        code2
        code3
        ...
}

所以 code1 只会在条件为真时 运行 但 code2code3 和其余的无论如何都会 运行。缩进在 C++ 中没有任何意义。我们可以(请永远不要这样做):

            if (condition)
    {
code1
code2
        code3
}

并且所有三行在 if 语句中都是 运行。

您的代码更正后方括号位于正确的位置

while (x != ranNum)
{
    if (x > ranNum)
    {
        cout << "Your input was greater than the system's generated random number. Please try again.\n";
        cin >> x;
        if (x > 100 || x < 1)
        {
            cout << "Input invalid. Please input a number between 1 - 100.\n";
            cin >> x;
        }
    }
    if (x < ranNum)
    {
        cout << "Your input was less than the system's generated random number. Please try again.\n";
        cin >> x;
        if (x > 100 || x < 1)
        {
            cout << "Input invalid. Please input a number between 1 - 100.\n";
            cin >> x;
        }
    }
    if (x == ranNum)
        cout << "You guessed right!\n";
}

我也将 while (x > ranNum || x < ranNum) 更改为 while (x != ranNum) 因为我们只想 运行 循环而 x 不等于 ranNum

您没有正确使用 if 语句:

{
    if (x > ranNum)
        cout << "Your input was greater than the system's generated random number. Please try again.\n";
    cin >> x;
    if (x > 100 || x < 1)
        cout << "Input invalid. Please input a number between 1 - 100.\n";
    cin >> x;
}

上述两个if条件都不会影响任何东西,除了是否打印输出消息,并且周围的花括号也没有用。当条件代码由多条语句组成时,需要将其用大括号括起来:

{
    if (x > ranNum)
    {
        cout << "Your input was greater than the system's generated random number. Please try again.\n";
        cin >> x;
    }
    if (x > 100 || x < 1)
    {
        cout << "Input invalid. Please input a number between 1 - 100.\n";
        cin >> x;
    }
}

此外,复制读取输入和执行范围检查的代码也没有意义。以下内容对我来说更直接:

int main()
{
    int ranNum;
    srand( time(0));

    ranNum = rand() % 100 + 1;

    cout << "Please input your guess for a random number between 1 - 100.\n";
    cin >> x;

    int x;
    for (;;) // Endless loop
    {                  
        cin >> x;

        if (x > 100 || x < 1)
        {
            cout << "Input invalid. Please input a number between 1 - 100.\n";
            continue; // Jump to next iteration of the loop
        }                                             

        if (x == ranNum)                              
        {
            cout << "You guessed right!\n";
            break; // Leave the loop
        }

        if (x > ranNum)
            cout << "Your input was greater than the system's generated random number. Please try again.\n";
        else
            cout << "Your input was less than the system's generated random number. Please try again.\n";
    }

    return 0;
}

您的代码可能如下所示:

#include <iostream>
#include <ctime>//A user stated that using this piece of code would make a true randomization process.
#include <cstdlib>
using namespace std;

int main()//When inputing a number, sometimes the user has to input it more than once.
{
    int x;
int ranNum;
srand(time(0));

ranNum = rand() % 100 + 1;

cout << "Please input your guess for a random number between 1 - 100.\n";


while (x > ranNum || x < ranNum)
{
    cin >> x;
    if (x > ranNum && x<=100 & x>=1) cout << "Your input was greater than the system's generated random number. Please try again.\n";
    else if (x < ranNum && x<=100 & x>=1) cout << "Your input was smaller than the system's generated random number. Please try again.\n";
    else if (x>=100 || x<=1) cout << "Input invalid. Please input a number between 1 - 100.\n";

}
cout << "You guessed right!\n";

    return 0;
}

未来:

当你使用 while 循环时,它包含了 if 语句。使用得当。另外,尝试 nt 嵌套 if 语句,但改用逻辑运算符。