-3
I’m trying to create a program in c# using windows Forms where it gets 3 numbers and tells me which is the biggest and which is the smallest, but I’m really very beginner and I don’t think where this error is, I already checked the list of errors of the IDE but this empty, it’s logic problem anyway. The code works but wrong.
Follow my code below:
double n1 = 0;
double n2 = 0;
double n3 = 0;
n1 = double.Parse(txtb1.Text);
n2 = double.Parse(txtb2.Text);
n3 = double.Parse(txtb3.Text);
if (n1 > n2 && n1 > n3)
{
MessageBox.Show("O maior número é: " + n1);
}
else if (n1 > n2 && n1 < n3)
{
}
else if (n1 < n2 && n1 < n3)
{
MessageBox.Show("O menor número é: " + n1);
}
else if (n2 > n1 && n2 > n3)
{
MessageBox.Show("O maior número é: " + n2);
}
else if (n2 > n1 && n2 < n3)
{
}
else if (n2 < n1 && n2 < n3)
{
MessageBox.Show("O menor numero é: " + n2);
}
else if (n3 > n1 && n3 > n2)
{
MessageBox.Show("O maior número é: " + n3);
}
else if (n3 > n1 && n3 < n2)
{
}
else if (n3 < n1 && n3 < n2)
{
MessageBox.Show("O menor numero é: " + n3);
}
"works but in the wrong way", how would be a "wrong way" to work? What values did you report and what was the result? I also noticed that you are using the
else if
in all conditions, so remember that aelse if
is executed only if no previous condition is met. In your case it will only display one message at a time.– Woss
I put, for example, 1, 5, 10 respectively but he always accuses that the first number is the largest ( in case 1) and does not tell me which is the smallest
– Gabriel Tonelli Maia
And if you put 5, 1, 10, it’s still the first number?
– Woss
yes, always the first number (if you want to put form print)
– Gabriel Tonelli Maia
Check that these variables are renamed correctly
txtb1.Text
,txtb2.Text
,txtb2.Text
.I tested with these values by placing directly on the variablesn1
,n2
,n3
and worked for the values 1, 5, 10– Bernardo Lopes
It has a lot of if and Else nested and this can make it difficult to check. Have you tried debugging the code? https://docs.microsoft.com/pt-br/dotnet/core/tutorials/debugging-with-visual-studio#:~:text=Press%20F5%20for%20run%20o,Start%20Debugging%20from%20the%20menu.
– Gabriel Santana
@Bernardolopes yes, they are, (n1, N2 and N3 are variables) (txtb 1, 2 and 3 are the textbox that receive the values
– Gabriel Tonelli Maia
@Gabrielsantana was worth it man I’ll take a look
– Gabriel Tonelli Maia
Check the text box boxes
– Bernardo Lopes
?? but they have no influence on the code... are normal there.
– Gabriel Tonelli Maia
but can for example is in form type NUMBER 1 and point to the variable txtb3
– Bernardo Lopes
No, they’re all in order...
– Gabriel Tonelli Maia
I suggest going in parts. First you try to show the largest and smallest without textbox (for example, with fixed values:
n1 = 1; n2 = 5; n3 = 10;
), because the way it is, you can’t tell if the problem is in reading the data or in its algorithm (or in a combination of both). Besides, the information you gave ("always accuses that the first number is the largest") does not proceed: see here a test with 1, 5 and 10 (informs that 1 is smaller) and here another with 5, 1 and 10 (does not print anything because it falls in one ofelse if
emptiness)– hkotsubo
In fact, so that one
else if
empty? If you do not want anything to be done in a certain condition, simply do not test that condition, that it is naturally ignored... That being said, if it’s only three numbers, you don’t need that lot ofif
/else
, you can do it smarter: https://answall.com/a/460749/112052 (this is in Python, but it’s not hard to adapt the idea to C#: https://ideone.com/Ahbm63) (of course, in practice, to sort several numbers, it is better to use what is already ready in the language, as already suggested in the answers)– hkotsubo