Error comparing numbers and checking if they are the same

3

I'm doing a simple C # exercise in which I have to get two numbers, compare them and print which one is the greatest or are the same.

When I start the program, I can only put the first number, and before placing the second, the program already returns the comparison, with numbers that have nothing to do with what I put.

For example: if I put 1 to the first, the program prints this:

 "Digite outro número: 49 é maior que 13.Pressione qualquer tecla para continuar. . ."

Am I using Read(); the wrong way? What would be another way to do this program?

class Program
{
    static void Main(string[] args)
    {
        Console.Write("Digite um número: ");
        int a = Console.Read();
        Console.Write("Digite outro número: ");
        int b = Console.Read();

        if (a > b)
        {
            Console.Write("{0} é maior que {1}.", a, b);
        }
        else if (b > a)
        {
            Console.Write("{0} é maior que {1}.", b, a);
        }
        else
        {
            Console.Write("Os dois números são iguais");
        }
    }
}
    
asked by anonymous 21.06.2016 / 18:15

3 answers

2

console.Read () is only one character. As you write a number and give an enter, in the variable it will be with the number you gave it, and in the variable with the value of the enter. You can do this:

 Console.Write("Digite um número: ");
        int a = Console.Read();
        Console.Read();
        Console.Write("Digite outro número: ");
        int b = Console.Read();
        Console.Read();

        if (a > b)
        {
            Console.Write("{0} é maior que {1}.", a, b);
        }
        else if (b > a)
        {
            Console.Write("{0} é maior que {1}.", b, a);
        }
        else
        {
            Console.Write("Os dois números são iguais");
        }

The best method to use in this case is

Console.ReadLine();

When using the method, you will have to convert the value that will be stored.

int.Parse(Console.ReadLine());
    
21.06.2016 / 18:23
8

The code does what you say, not what you want. In case you are using the wrong method. Read() is for reading a single character. To read more is ReadLine() .

There you can think, but it returns a text and I want a number. Yes, you have to do the conversion, probably with Tryparse() . .

using static System.Console;

public static class Program {
    public static void Main(string[] args) {
        Write("Digite um número: ");
        var texto = ReadLine();
        int a;
        if (!int.TryParse(texto, out a)) {
            Write("Deu erro");
            return;
        }
        Write("Digite outro número: ");
        texto = ReadLine();
        int b;
        if (!int.TryParse(texto, out b)) {
            Write("Deu erro");
            return;
        }

        if (a > b) {
            Write($"{a} é maior que {b}.");
        } else if (b > a) {
            Write($"{b} é maior que {a}.");
        } else {
            Write("Os dois números são iguais");
        }
    }
}

See working on dotNetFiddle .

Obviously this code can be improved. You can create a method to generalize data entry and avoid repetitive code. You can do something better than just say you made a mistake. You can make a loop by asking for the dice again. It's there as an exercise.

Note that I updated the code with C # 6 thing.

I do not recommend using Parse() . It can generate errors if the text can not be converted. Try placing a text and see what happens with this method. And see because Tryparse() is better .

    
21.06.2016 / 18:22
3

Change the read to readline with parse (it will read after giving enter): Console.Write ("Enter a number:");

Console.Write("Digite um número: ");
int a = int.Parse(Console.ReadLine());
Console.Write("Digite outro número: ");
int b = int.Parse(Console.ReadLine());
    
21.06.2016 / 18:21