Need help understanding cycles

Well, what I'm trying to do seems pretty simple, but it doesn't work the way I want it. I know that I'm just not getting anything. In fact, I'm trying to read console input, assign it to a variable. Then I want to check this variable to see if it is a valid number. If this is not the case, I want to tell the user that it is not valid, and run the loop again until I get a real number, and then exit. Here is my code, could you help me understand what I am doing wrong?

    const int AVERAGE_IQ = 100;
    int userIQ;
    bool done = false;

    do
    {
        Console.Write("Please enter an IQ Score between 1 and 200: ");
        userIQ = Convert.ToInt32(Console.ReadLine());

        if (userIQ == 0 || userIQ >= 200)
        {
            Console.WriteLine("You have entered an invalid IQ Score, please try again.");
            done = false;
        }
        else if (userIQ >= AVERAGE_IQ)
        {
            Console.WriteLine("{0} is an above average IQ.", userIQ);
            done = true;
            break;
        }
        else if (userIQ <= AVERAGE_IQ)
        {
            Console.WriteLine("{0} is an below average IQ.", userIQ);
            done = true;
            break;
        }
        else if (userIQ == AVERAGE_IQ)
        {
            Console.WriteLine("{0} is anaverage IQ.", userIQ);
            done = true;
            break;
        }

    } while (done =! true);
+3
source share
8 answers

=! it should be !=

You are doing a task that is set to false. Because the:

while (done =! true);matches with while (done = !true);that matches withwhile (done = false);

, false, , .

while (done != true); while (!done);

+16

:

while (done =! true);

, , done !true, false, done false, .

:

while (!done);

, break , , done, :

while (true);
+6

done =! true done != true

+4

. , . done break. (while done);

+3
  • IQ < 0

  • >= >, <= <

  • done while while (true)

+2

:

  • else if (userIQ >= AVERAGE_IQ) else if (userIQ > AVERAGE_IQ) " "
  • break; , done=true
0

" , "? ? ? ?

, . >= AVERAGE_IQ <= AVERAGE_IQ == AVERAGE_IQ. AVERAGE_IQ, , , IQ. The >= <= > < .

, done , . while (true), .

, . .

: , "=!" , "! =".

0
source

In this loop, the task is to force the user to enter the correct input. An assessment that input outside the appropriate range should be moved to another location.

If you rebuild your code for something like this, it is more clear what you are trying to do.

int minIQ = 1;
int maxIQ = 200;
do
{
    Console.Write("Please enter an IQ Score between " + minIQ + " and " + maxIQ + ": ");
    userIQ = Convert.ToInt32(Console.ReadLine());
} while (userIQ >= minIQ && userIQ <= maxIQ);

//Test userIQ and output here.
0
source

Source: https://habr.com/ru/post/1792320/


All Articles