How do I receive a value for each double variable on the console?

Asked

Viewed 590 times

3

I was making a very simple code and I didn’t want to type a number and then enter it and then enter another number in another line, I wanted to know if I can do it in the same.

        Console.WriteLine("informe o comprimento e a largura da area em metros");

        comprimento = Convert.ToDouble(Console.ReadLine());

        largura = Convert.ToDouble(Console.ReadLine());

        area = largura * comprimento;

        Console.WriteLine("A area é de" + area+ "m²");

        Console.ReadKey();

This way I have to type the values in different lines, how do I type in the same line ?

  • you can enter all the values separated by comma and then do the Split. has several examples here on the site, such as: https://answall.com/q/38521/57220

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site

2 answers

3


Directly you cannot do this with the infrastructure . NET provides. Either you have to access the console directly from the API of the operating system, which is quite complicated, or you have to resort to tricks (for example parse what was typed, but one has to type right and a lot of things can go wrong, don’t do this).

Better yet, you can do the right thing and not put this requirement in your software, it is bad from the point of view of UX, the right thing is to ask for the data separately, including asking for the text one at a time, this way is asking for something to go wrong. And it looks like you’re not worried about mistakes because this conversion will potentially break the application, that’s not how it’s done. Something like this is more right:

using static System.Console;

public class Program {
    public static void Main() {
        Write("Informe o comprimento em metros: ");
        if (!double.TryParse(ReadLine(), out var comprimento)) return;
        Write("Informe a largura em metros: ");
        if (!double.TryParse(ReadLine(), out var largura)) return;
        WriteLine($"A area é de {largura * comprimento} m²");
    }
}

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

I had it closed if the data isn’t valid, but it could give an error message, ask again, or do whatever you think is best. If you go the way of trying to interpret two data in a row only a lot can go wrong and your code would be too complex to handle all cases, then make it simple, including because it is more user friendly.

  • If the TryParse return false, largura will be null or will no longer be declared? Either way, the next line will cause an error if it does not have the condition if(!(largura is null)) ... ;

  • 1

    Neither the one nor the other, it happens to have the value default, in case 0, but it is important that you do not use this value in case of false.

  • If the user reports zero, how to distinguish if it was understood correctly or if the default value of the variable was assigned?

  • 2

    It is not to distinguish anything, if the person typed 0 is a valid value and can use, if the person typed something wrong, the variable has value 0, but I will repeat, it is not to use this value. You just received a fake indicating that the value is not valid because you would use it? You have already had your statement that it is not to use. It’s what’s in C#, otherwise it can create a mechanism of Option and make your own TryParse() return.

0

        string[] dados = null;
        double area = 0;

        Console.WriteLine("informe o comprimento e a largura da area em metros");//o usuario digita SOMENTE os valores EX: 4 4

        dados = Console.ReadLine().Split(' ');//usa o split para cortar a string e atribuir os valores na array

        area = double.Parse(dados[0]) * double.Parse(dados[1]); //converte, calcula e atribui 

        Console.WriteLine("A area é de " + area + " m²");`insira o código aqui`

Browser other questions tagged

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