0

my simple command to check wh is the biggest number, is not working with 9 and 8 in visual studio with C#

  using System.ComponentModel.Design;

  class Program
  {

    static void Main()
    {
        int n1;
        int n2;
        int n3;

        Console.WriteLine("Escreva o primeiro número: ");

        n1 = int.Parse(Console.ReadLine());

        Console.WriteLine("Escreva o segundo número: ");

        n2 = int.Parse(Console.ReadLine());

        Console.WriteLine("Escreva o terceiro número: ");

        n3 = int.Parse(Console.ReadLine());


        if (n1 > n2 & n1 > n3)
        {
            Console.WriteLine("O maior número entre os 3 é o PRIMEIRO");
        }
        else if (n2 > n1 & n2 > n3)
        {
            Console.WriteLine("O maior número entre os 3 é o SEGUNDO");
        }
        else if (n3 > n1 & n3 > n2)
        {
            Console.WriteLine("O maior número entre os 3 é o TERCEIRO");
        }
        else if (n1 == n2 & n2 == n3)
        {
            Console.WriteLine("Todos os 3 números são IGUAIS!");
        }

        Console.ReadLine();
        
    }
  }

Until then it worked perfectly, I just can't use these numbers repeatedly

CMD requests 3 numbers, if they are 9 or 8 the result simply does not appear

First number 99, second number 88 and third number 99:

enter image description here

But, does not return anything

Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
  • 4
    I'm no C# expert but shouldn't those `&` operators be `&&`? I think `&` is a bitwise AND whereas `&&` is the (required) logical AND. – Adrian Mole Feb 28 '23 at 14:24
  • Consider [using debugger to step through your program](https://stackoverflow.com/a/25385174/7034621). Logically, there are 6 possible orders of 3 variables and your program covers only 4 of them. – orhtej2 Feb 28 '23 at 14:24
  • @AdrianMole for booleans the result of `&&` and `&` will be the same, the difference being bitwise always evaluates left and right operand. I doubt this is the problem here, [`&` is still evaluated after comparison](https://learn.microsoft.com/en-us/cpp/c-language/precedence-and-order-of-evaluation?view=msvc-170). – orhtej2 Feb 28 '23 at 14:28
  • 1
    @AdrianMole: the difference between `&` and `&&` is irrelevant here. The problem is that none of the combined conditions allow for the combination where two of the values are the same and different from the third value. – Johan Donne Feb 28 '23 at 14:30
  • Check your conditions for n1 = 99, n2 = 88 and n3 = 99 (and keep in mind the effect of the logic 'and'!). – Johan Donne Feb 28 '23 at 14:32
  • @JohanDonne Actually a very good point. Maybe LarsTech can reopen this? – Adrian Mole Feb 28 '23 at 14:36
  • @LarsTech Care to reconsider the duplicate closure, in the light of the comments about the operators' equivalence in this case? – Adrian Mole Feb 28 '23 at 14:37
  • Also, I can't really use the fact that I'm really a C and C++ programmer as an excuse: The relative precedence of the `>`, `&` and `&&` operators are very similar in those languages to what they are in C#. – Adrian Mole Feb 28 '23 at 14:38
  • Why don't you put numbers into a collection (say, array). `Sort` then and then print? – Dmitry Bychenko Feb 28 '23 at 14:59
  • Your conditions work if either all numbers are equal or the maximum is unique. They don't work if you enter the maximum twice, because none of them is greater then both others. – derpirscher Feb 28 '23 at 15:12
  • The simplest way to fix this in your existing code is probably to replace `>` with `>=` – derpirscher Feb 28 '23 at 15:20

1 Answers1

1

First of all, you probably should be using && (conditional logical AND) instead of & (logical operator AND). It won't make any difference in your case, but can make in future ones.

Debugging would help you understand what's going on.

Check this out:

if (n1 > n2 & n1 > n3) -> if (99 > 88 & 99 > 99) -> false

else if (n2 > n1 & n2 > n3) -> else if (88 > 99 & 88 > 99) -> false

else if (n3 > n1 & n3 > n2) -> else if (99 > 99 & 99 > 88) -> false

else if (n1 == n2 & n2 == n3) -> else if (99 == 88 & 99 == 88) -> false

All of your conditions are evaluating to false, that's why it's not printing any messages.

Anderson Pimentel
  • 5,086
  • 2
  • 32
  • 54