Error comparing numbers and checking if they are equal

Asked

Viewed 932 times

4

I’m doing a simple C# exercise in which I have to receive two numbers, compare them and print out which is the largest or whether they are equal.

When I start the program, I can only put the first number, and before putting 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. . ."

I’m using the 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");
        }
    }
}

3 answers

8

The code does what you tell it to do, not what you want. In this case you are using the wrong method. o Read() is to read a single character. To read several is the ReadLine().

Then 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: ");
        if (!int.TryParse(ReadLine(), out var a)) {
            Write("Deu erro");
            return;
        }
        Write("Digite outro número: ");
        if (!int.TryParse(ReadLine(), out var 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");
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

Obviously this code can be improved. It 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 asking for the dice again. Here’s how to exercise.

Note that I modernized the code with C# 6.

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

  • Very well put: "the code does what you say, not what you want!" This helps to improve the quality of the questions!

3

Change the read to readline with parse (you will read after you 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());
  • 4

    int.Parse it’s not cast, it’s conversion.

  • I corrected the text and gave a vote for your comment. Thank you for the remark.

3


console. Read() read only one character. As you write a number and enter, in Variabel he gets the number you gave him, and Variabel b gets the value of enter. You can do it like 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 need to convert the value that will be stored.

int.Parse(Console.ReadLine());
  • 3

    And if the user type the letter A?

  • With 'A', I would give exception! P.S.: I just answered him the question he asked!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.