6
I made a simple C# code that asks you to enter name, age, weight, city and state, performs normally only that it appears some problems as instead of the weight being 60.5KG gets 605KG and wanted to know how to break the line to print the part filling the form and printing separately.
// Meu Código
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TerceiroCd
{
class Program
{
static void Main(string[] args)
{
String nomeNome, cidadeC, estadoE;
double peso;
int idade;
Console.Write("Digite o seu nome: ");
nomeNome = Console.ReadLine();
Console.Write("Digite a sua idade: ");
idade = int.Parse(Console.ReadLine());
Console.Write("Digite o seu peso: ");
peso = double.Parse(Console.ReadLine());
Console.Write("Digite sua cidade: ");
cidadeC = Console.ReadLine();
Console.Write("Digite o seu estado: ");
estadoE = Console.ReadLine();
Console.WriteLine($"Seu nome é {nomeNome}");
Console.WriteLine($"Possui {idade} de idade e pesa {peso}KG");
Console.WriteLine($"Mora na cidade de {cidadeC} no estado de {estadoE}");
Console.ReadKey();
}
}
}
// A impressão fica assim
Digite seu nome: Alex F.
Digite sua idade: 20
Digite o seu peso: 60.5
Digite sua cidade: Guanambi
Digite o seu estado: Bahia
Seu nome é Alex F.
Possui 20 anos de idade e pesa 605KG // Valor errado
Mora na cidade de Guanambi no estado da Bahia
// Queria que fosse assim:
Digite seu nome: Alex F.
Digite sua idade: 20
Digite o seu peso: 60.5
Digite sua cidade: Guanambi
Digite o seu estado: Bahia
Seu nome é Alex F.
Possui 20 anos de idade e pesa 60.5KG
Mora na cidade de Guanambi no estado da Bahia
My variable weight when typed 60.5 appears 605 in print, how does it solve this?
– user141036
I cannot reproduce the problem, the application breaks every time and the answer already talks about it.
– Maniero
But the code runs, it’s only showing the weight value wrong
– user141036
I edited, you have to do something else not to accept the point.
– Maniero
If you put 60.5 goes normal, but real numbers in programming do not use comma
– user141036
That’s not programming, that’s data input.
– Maniero
You can replace "." and replace it with ",". Example: weight = double.Parse(Console.Readline(). Replace(".",","))
– Lucas
@Lucas this is gambiarra, it’s a way to adjust if the person goes wrong, but it generates an ambiguity, what if the person uses a thousand separator? People never think about real use. It’s what I always say, people don’t want to do the right thing, they just want to see it work, then someone does something out of the ordinary and it’s trouble. It is even possible to do a personalized analysis and give a message to the person indicating that probably she is typing wrong as a tip to enter the data correctly, but not as validation, and mainly not as a change cumpulsória and automatic.
– Maniero
In this case there is no problem. But its data entries need to be validated correctly. You could do this using a Tryparse
– Lucas
It generates problem yes, the fact that you do not see problem does not mean that it does not generate, I wrote a case that generates problem.
– Maniero
@Lucas using Replace() worked, but could be a problem in the future.
– user141036
which? It will cause problems if you type an alphanumeric string. What can happen with other inputs. But if validate with a Tryparse() from to treat this. Any different type of string can generate error when entering an input, it is important to validate the input types
– Lucas
@Alexf. Exactly what I said, it worked a case, not all, https://i.stack.Imgur.com/jgk8Q.png
– Maniero
@Lucas went to try to get the comma out by only getting one point, but the IDE accused that Replace() needs 2 arguments to work.
– user141036
Yes, the Replace() method uses 2 arguments. The first indicates the string you want to replace, the second indicates the string that will be in place. If you do not find the string to be replaced, it changes nothing.
– Lucas
"In this case there is no problem" = gambiarra. I recommend that people who insist too much on this line of reasoning try something in the area of humanities, like fine arts, theater, and other things that give space for personal interpretations. For exact it is kind of risky to work like this, it harms the client (it is even better to rethink more widely and improve the procedures, of course).
– Bacco
@Maniero, you like the solution, show! Sure it’s better this way, but otherwise he wouldn’t have a problem either, if he used Tryparse(), as you did.
– Lucas
@Bacco, thanks for the feedback. I just tried to help the friend, this was the solution I had in mind. Now, I learned a new technique from Maniero.
– Lucas
@Lucas, quite rightly, proposing something that may not be ideal happens quite often and with a lot of people, what you cannot do is settle for the solution and stay in it. As long as you understand the problem that it generates and improve with each "iteration", everything is within normality. Obviously, in our things, we take the liberty of going out of our way to solve immediate problems, but whenever teaching a third party, it is important to make the problem clear (or really focus only on the proper technical way)
– Bacco