1
My code should be entered the gender of a person, height and age of a person in a repeat loop for 10 people.
The code must show the greater weight, lower weight, average height of 10 people, the amount of men and the gender of the heaviest person.
But there is an error in the use of the unassigned local variable Strings
sexo, pessoamaispesada
and wanted to know how to fix it
Following the lines of my code that appear the variables sexo, pessoamaispesada
// Declarando as strings
string sexo, pessoamaispesada;
// Parte que pergunta o gênero da pessoa
for (i = 0; i < 10; i++)
{
Console.Write($"Digite o sexo da pessoa {i} (M - masculino | F - feminino): ");
sexo = Console.ReadLine();
}
/// Capturar a pessoa de determinado genero com maior peso
for (i = 0; i < 10; i++)
{
if (peso > maiorpeso)
{
maiorpeso = peso;
sexo = pessoamaispesada; // Uso da variável local não atribuída "pessoamaispesada"
}
}
// Contabiliza a quantidade de homens
for (i = 0; i < 10; i++)
{
if (sexo == "M" || sexo == "m") // Uso da variável local não atribuída "sexo"
{
qnthomens++;
}
}
// Imprimindo a pessoa mais pesada de determinado gênero
Console.Write($"A pessoa mais pesada é {pessoamaispesada}");
It was a bit confusing understanding. Can you post the full code? So you can better see the problem.
– Fabiano Salles
Your code could be posted in its entirety?
– novic
@Virgilionovic already posted.
– Carlos A.
Reversed: First you claimed in question the following statement:
string sexo, pessoamaispesada;
, in the edition you putstring sexo = ""; var pessoamaispesada = "";
thus invalidating the answers.– Augusto Vasques
Actually @Augustovasques reversed may even invalidate the answers, but the problem was obscure and it wasn’t because of that. The problem was more internal and in my opinion should have let the user put the whole of the code that yes we could point out the real problem by an answer. The question is bad, it’s a do for me because I lack user experience and I think users post question and think we have to guess I for example hope they put as much information before answering.
– novic