user3311305
user3311305

Reputation: 1

Testing Values For Multiple Conditions in C#

I am having some difficulty finding out what's wrong with my code. I am trying to create a console app in C#. The program is supposed to ask the user to to input 3 numbers. All of the number must be greater than 0. The first number should be even, the second should be whole, and the third should be odd. My syntax seems to be correct, however, when I run the program, it seems to ignore the if (userInput > 0) part. Here is my code:

class Program
{
    static void Main(string[] args)
    {
        try
        {
            int userInput;
            Console.WriteLine("Please enter an even number.");
            userInput = Convert.ToInt32(Console.ReadLine());
            if (userInput > 0 && !isEven(userInput))
                return;
            Console.WriteLine("Please enter a whole number.");
            userInput = Convert.ToInt32(Console.ReadLine());
            if (userInput > 0)
            Console.WriteLine("Please enter an odd number.");
            userInput = Convert.ToInt32(Console.ReadLine());
            if (userInput > 0 && isEven(userInput))
                return;
            Console.WriteLine("Congratulations!");
            Console.WriteLine("Press any key to continue.");
            Console.ReadKey();
        }
        catch { }
    }

    static bool isEven(int value)
    {
        if (value % 2 == 0)
            return true;
        else
            return false;
    }       
}

If anyone could tell me how to properly test both conditions I would be eternally greatful. Thanks.

Upvotes: 0

Views: 1007

Answers (4)

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112752

Check

if (userInput <= 0 || !isEven(userInput))
    return;

Since you want to quit when either the number is not positive OR the number is not even.


De Morgan's laws tell you how you can invert a logical expression. You do it by replacing each term by it's negated term and by replacing the AND's by OR's and vice versa.

So you could also write

if (userInput > 0 && isEven(userInput)) {
    // The user input is ok, continue
} else {
    return;
}

userInput <= 0 is the negation of userInput > 0.

De Morgan's laws

NOT(A AND B) = NOT(A) OR NOT(B)

and

NOT(A OR B) = NOT(A) AND NOT(B)

Upvotes: 0

IllusiveBrian
IllusiveBrian

Reputation: 3234

Based on the way you've structured your program, there is likely supposed to be a return statement after the second if, IE:

if (userInput > 0)
    return;

However, this still does not address the problem that what you are probably trying to say is:

if (userInput <= 0)
    return;

Or somewhat more directly translated from English:

if (!(userInput > 0))
    return;

This change of sign applies to your other if statements as well, as others have mentioned.

Upvotes: 0

Sudhakar Tillapudi
Sudhakar Tillapudi

Reputation: 26209

Problem : you are checking for one Valid and other invalid scenario with Logical AND.

Solution : you need to use both invalid scenarios with Logical OR.

asper your requirement program should not proceed if any one of the following rule breaks:

a. userinput should be > 0
b. userinput should be odd,whole and even

1. Replace This:

if (userInput > 0 && !isEven(userInput))

With This:

if (userInput <= 0 || !isEven(userInput))

2. Replace This:

if (userInput > 0 && isEven(userInput))

With This:

if (userInput <= 0 || isEven(userInput))

Upvotes: 1

Servy
Servy

Reputation: 203848

Your requirements for the first number entered are essentially:

The number must be both positive and even.

The inverse of that is:

If the number is not positive, OR it is not even, then it is not valid.

Your check says

If the number is positive AND it is odd, then reject it

Meaning any number that is zero, negative, or even is valid.

What we have here is an application of DeMorgan's Law. It states that:

!(A && B)

is equivalent to

!A || !B

So if a valid number is even AND positive, then an invalid number is not even OR not positive (not positive is less than or equal to zero).

Upvotes: 3

Related Questions