0

I tried the following , I want to get a message if the entered number isn't 1, 2 or 3 :

Console.WriteLine("which number?");
int number = Convert.ToInt32(Console.ReadLine());
if (number != 1 || number != 2 || number != 3)
{
    Console.WriteLine("wrong number!");
}

but also if my input is 1,2 or 3 I get the message , but why ? whats wrong ? :/ there is no error by visual studio.

1
  • 2
    use && - "And" operator Commented Dec 1, 2016 at 17:13

4 Answers 4

1

You should use AND operator in that if statement

if (number != 1 && number != 2 && number != 3)
{
    Console.WriteLine("wrong number!");
}

or in this case you can use

if (number <= 0 || number >= 4)
{
    Console.WriteLine("wrong number!");
}
Sign up to request clarification or add additional context in comments.

1 Comment

Your second answer will not work, if he enters -4 then it will result in a valid number, however your first case is correct
0

You should consider using && operator which will check for at least number does not belong to one of those

if (number != 1 && number != 2 && number != 3)
{
    Console.WriteLine("wrong number!");
}

Comments

0

You're confusing English with C#.

TL;DR: Use the && operator to check for all three, instead of just checking for any of the three.


You're currently checking: if the number is either not one, or not two, or not three, then say "wrong number".

if (number != 1 || number != 2 || number != 3)

Take, for example, number = 1. The code will first check whether number != 1, and will return false. However, since you're using an OR statement, it will check the next one. What ends up happening is this:

if (false || true || true)

Since you're using OR, C# checks to see if ANY of those are true. So this further simplifies to the following:

if (true)

Since a number can not be !=1, !=2, and !=3 at the same time, this will always have at least two trues. Therefore, if you use the OR operator, the expression will always return true

What you want is the && operator.

//if number is NOT 1 AND NOT 2 AND NOT 3
if (number != 1 && number != 2 && number != 3)

With the example above, this would simplify to this:

if (false && true && true)

Which goes further to:

if (false)
    //wrong number

Which will skip the inside, because 1 is a right number. If 1, 2, and 3 are wrong numbers, just put an exclamation point before the entire block, as follows:

if (!(number != 1 && number != 2 && number != 3))

Comments

0

If I take a look at your code

Console.WriteLine("which number?");
int number = Convert.ToInt32(Console.ReadLine());
if (number != 1 || number != 2 || number != 3)
{
    Console.WriteLine("wrong number!");
}

lets say number is now equal to 1 if (1 != 1 || 1 != 2 || 1 != 3) this will give the following using Truth tables if ( false || true || true ) this will then result in if (true)

For more information about truth tables you can visit http://kias.dyndns.org/comath/21.html but in short using the stroke

true || true = true

true || false = true

false || true = true

false || false = false

But if you look at using the ampersand true && true = true

true && false = false

false && false = false

This is the reason why you will always get to line Console.WriteLine("wrong number!"); because if you enter 1,2,3 you will get atleast 2 'True' values, you have a number of options that you can follow

1) if (number != 1 || number != 2 || number != 3) changes to if (number != 1 && number != 2 && number != 3) 2) make an array for valid values int[] valid = new int[] {1,2,3} Then test if the input is inside of the valid array using a for loop or the easy Linq method Contains(...)

Console.WriteLine("which number?");
int number = Convert.ToInt32(Console.ReadLine());
if (!valid.Contains(number))
{
    Console.WriteLine("wrong number!");
}

Note I have added a Not operator infront of valid (!), this means if valid does not contain the value

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.