开发者

Need help understanding loops

开发者 https://www.devze.com 2023-02-10 11:51 出处:网络
Ok what I am trying to do seems fairly simple, yet it\'s not working the way I want it. I know I\'m just not getting something. Essentially I am trying to read console input, assign it to a variable.

Ok what I am trying to do seems fairly simple, yet it's not working the way I want it. I know I'm just not getting something. Essentially I am trying to read console input, assign it to a variable. Then I want to check that variable to see if it is a valid number. If it's not, I want to tell the user it is invalid and start the loop over again until I get a valid number, then exit. Here is my code, can you please 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);
开发者_JAVA百科            done = true;
            break;
        }
        else if (userIQ == AVERAGE_IQ)
        {
            Console.WriteLine("{0} is anaverage IQ.", userIQ);
            done = true;
            break;
        }

    } while (done =! true);


=! should be !=

You're doing an assignment, setting done to false. Since:

while (done =! true); is the same as while (done = !true); which is the same as while (done = false);

On each iteration, you're assigning done to be false, which itself evaluates to false, which means your loop will never iterate a second time.

Change the loop expression to either while (done != true); or better yet while (!done);


The error is in the last line:

while (done =! true);

What you are doing is assigning done !true which is false so regardless of the value of done before this statement executes, it will turn to false and the loop will exit.

What you really want is:

while (!done);

Also, as you are using break to break out of the loop, you can remove every line where done variable is used and just use this line without any problems:

while (true);


while done =! true should be changed to while done != true


As xanatos says. However, there is a style issue of your break vs. your done flag. The alternative to break, would be to keep the done flag - and remove the break statements. Your while would then become while (!done);


  1. Check for IQ < 0

  2. Change >= to >, <= to <

  3. Delete all the done and change the while to while (true)


A few notes:

  1. Change the else if (userIQ >= AVERAGE_IQ) to else if (userIQ > AVERAGE_IQ) Since you have the last "if they are equal"
  2. Since you use break; to exit the loop, you do not need to set done=true


What do you mean by "not working the way I want it"? Is it not prompting the user for input correctly? Is it displaying the incorrect message? Is it throwing an exception?

At first glance, I already see a bug. You check for >= AVERAGE_IQ then <= AVERAGE_IQ then == AVERAGE_IQ. If the user were to enter in the AVERAGE_IQ, the first condition would hit and it would say it's an above average IQ. The >= and <= should be > and < instead.

Also, the done flag is redundant since you're breaking. You might as well just use a while (true) loop if you're going to use breaks.

Another thing, you aren't handling negative input correctly. It should also display that out of range error message.

Edit: as many other have pointed out, "=!" is not the same thing as "!=".


The loop is there to deal with getting the user to enter the correct input. Evaluating that input beyond being in the appropriate range should be moved somewhere else.

If you restructure your code to 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

精彩评论

暂无评论...
验证码 换一张
取 消