Code skips the error if statements and goes straight to the else if
I need laps to go through and if its less than 2 then it'll go to the error and come back again to ask to enter a new value. vice versa for greater than 20. Im a new programmer and find C# Windows Forms hard to understand
int.TryParse(txtNumberOfLaps.Text, out laps);
while (laps < 2 && laps > 20)
{
if (laps < 2)
{
MessageBox.Show("Laps can't be less than 2", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else if (laps > 20)
{
MessageBox.Show("Laps can't be more than 20", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else if (laps > 2 && laps < 20)
{
break;
}
}
CodePudding user response:
From what I'm seeing, I think your code doesn't enter the while loop. You are asking laps < 2 AND laps > 20
while I think what you would want would be laps < 2 OR laps > 20
. Your loop doesn't make sense because your number can't be bigger than 20 and lesser than 2 at the same time
CodePudding user response:
Which else if
does it skip to? Your loop contains two. I imagine the program is exiting before even starting the loop due to the loop's sentinel value being insatiable. For any given integer n
, 20>n<2
doesn't exist. It is literally impossible. You can see this by checking what number(s) would make each condition of your while
loop true. For example
n < 2
can most easily be satisfied by supplying a1
.
n = 1
makesn > 20
false, however.
on the other side,
n > 20
is most easily made true by supplying21
as an argument.
n = 21
makes your first condition ofn<2
false and thus your loop never begins
So, in any case of n
, your while
loop never receives an initial True
value to even begin.
Now that that's settled, let's get you a solution! :)
What you are looking for would be something like this:
int.TryParse(txtNumberOfLaps.Text, out laps);
while(true)
{
if (laps < 2 || laps > 20)
{
MessageBox.Show("Laps must be in range 2-20", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
else
{
// do other things
break;
}
}
CodePudding user response:
The while loop is never entered because a number can't be both less than 2 and more than 20. laps < 2 && laps > 20
should probably be laps < 2 || laps > 20
Correcting that, if the number is below 2 or above 20, the while loop is entered, but the last else-if statement is never true.
If the number is within range, the while loop is never entered, and thus none of the if statements will be hit.
Here is an example on how you could solve the problem you describe.
while (true)
{
Console.WriteLine("Please enter a number between 2 and 20:");
var input = Console.ReadLine();
if (!int.TryParse(input, out var number))
{
// the input could not be parsed
Console.WriteLine("Please enter a proper numeric value.");
}
else if (number < 2)
{
Console.WriteLine("The number can't be less than 2");
}
else if (number > 20)
{
Console.WriteLine("The number can't be more than 20");
}
else
{
// if all if-statements above is false, the number must be between 2 and 20
Console.WriteLine($"You entered {number}. Good job!");
break;
}
}
This looks like one of those early problems you get to solve when you first learn programming. It's sort of like a trick-question, where there is multiple flaws for you to discover.
CodePudding user response:
The problem is here:
while (laps < 2 && laps > 20)
It cannot be below 2 and above 20 at the same time so the loop never executes. Use ||
instead of &&
and it will work.
Also once it enters the loop, the final else if
statement is redundant and can be removed. If that condition is ever true it will break from the loop anyway.
This code would work:
int.TryParse(txtNumberOfLaps.Text, out laps);
while (laps < 2 || laps > 20)
{
if (laps < 2)
{
MessageBox.Show("Laps can't be less than 2", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
MessageBox.Show("Laps can't be more than 20", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}