Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Second if statement isnt working, how can i fix it?

Console.WriteLine("Enter first number:");
double x = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Enter second number:");
double y = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Enter third number:");
double z = Convert.ToInt32(Console.ReadLine());

if (z > x && y < z)
{
    Console.WriteLine("The third number is not smallest number of the three");
}


if (z < x && y > z)
{
        Console.WriteLine("The third number is the smallest of the three");


What I have tried:

Using different statements. Tried using else and else if.
Posted
Updated 20-Oct-21 0:31am

It looks like your first statement, not the second, is incorrect.

For z not to be the smallest number, it only has to be greater than x or greater than y. So the first statement should be
C#
if ((z > x) || (z > y))
I prefer to use the extra parentheses to make things obvious, and to use the same order on both sides if this makes the logic clearer.

For z to be the smallest number, it must be less than x and less than y. I would write this as
C#
if ((z < x) && (z < y))
But that's the same as what you have, only with the second comparison flipped for better clarity.
 
Share this answer
 
Comments
Member 13566383 20-Oct-21 1:31am    
How do you know what the questioner intended to do? I hate this "is not working"-questions, lacking any further information.
OriginalGriff 20-Oct-21 1:57am    
How? By reading the code! :laugh:
If the first condition in the OP code passes, it prints a message - that message tells you what the OP meant to do: find out if z was the smallest.

Newbies tend to post "information free" questions, because they aren't used to a field where there is no "one right answer" but as many right answers as developers doing it. They are used to maths homework where you have to answer with the one right answer, so they assume that the dev homework they have been given is the same, and that we have all been set the same question. So they don't think to post the context around their code and we often have to infer meaning from content ...

Me? I hate the ones where they just copy'n'paste the question and expect us to do all the work for them. Lazy gits. :D
Greg Utas 20-Oct-21 8:17am    
Member 13566383 kind of has a point. Maybe the bug was in the WriteLine!
To add to what Greg has said, you can simplify your code pretty easily.
You code is trying to print one message if z is not the smallest, and another if it is: so make the code reflect that: if it passes one test it can't pass the second because a number can't be both the smallest and not the smallest. Additionally, your code doesn't do anything at all if two number are equal and the smallest.

So try using an else clause:
C#
if ((z < x) && (z < y))
   {
   Console.WriteLine("The third number is the smallest of the three");
   }
else
   {
   Console.WriteLine("The third number is not the smallest of the three");
   }

It's also a good idea not to use Convert methods on user input - everybody makes mistakes and using Convert will cause your app to crash if the use miskeys. Instead, use double.TryParse and report an error instead:
C#
bool valid;
double x;
do
   {
   Console.Write("Enter first number: ");
   string input = Console.ReadLine();
   valid = double.TryParse(input, out x);
   if (!valid)
      {
      Console.WriteLine($"\"{input}\" is not a number! Please try again.");
   } while !valid);
It's not a massive problem with only three numbers to input, but when you get to number 20 of 21 and the app crashes because you the wrong key, the user is going to be rather pissed off...
 
Share this answer
 
I'd simplify this, and include the cases where:

1) x,y,z are the same value
2) z is the same value as either x, or y
3) z is between x and y
public enum ZState
{
    ZBetweenXY,
    ZXYEqual,
    ZEqualXOrY,
    ZSmallest,
    ZLargest
}

public ZState GetZState(double x, double y, double z)
{
    if (z == x || z == y)
    {
        if (x == y)
        {
            return ZState.ZXYEqual;
        }
        else
        {
            return ZState.ZEqualXOrY;
        }
    }

    if (z > x && z > y)
    {
        return ZState.ZLargest;
    }
    else if (z < x && z < y)
    {
        return ZState.ZSmallest;
    }

    return ZState.ZBetweenXY;
}

/* tests
var tst1 = GetZState(99, 99, 99);
var tst2 = GetZState(99, 99, 100);
var tst3 = GetZState(-99, -99, -100);
var tst4 = GetZState(-99, 99, -99);
var tst5 = GetZState(99, -99, -99);
var tst6 = GetZState(99, 102, 101);*/
Note: with comparing Doubles, there is a danger of round off errors: in real world code this should be handled: [^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900