I'm really not sure how you are trying to accomplish this, but I think I see your problem. Each of your
if
statements is checking to see if the value is
not equal to the specified number. Change each of these to equal and you should have a working solution. Here is what it should look like:
private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
{
number1.Text = rnd.Next(Convert.ToInt32(1), Convert.ToInt32(10)).ToString();
number2.Text = rnd.Next(Convert.ToInt32(1), Convert.ToInt32(10)).ToString();
sign.Text = rnd.Next(Convert.ToInt32(1), Convert.ToInt32(5)).ToString();
if (sign.Text == "1")
{
float n1 = float.Parse(number1.Text);
float n2 = float.Parse(number2.Text);
float result = n1 * n2;
answer.Text = result.ToString();
}
else if (sign.Text == "2")
{
float n1 = float.Parse(number1.Text);
float n2 = float.Parse(number2.Text);
float result = n1 + n2;
answer.Text = result.ToString();
}
else if (sign.Text == "3")
{
float n1 = float.Parse(number1.Text);
float n2 = float.Parse(number2.Text);
float n3 = n1 * n2;
number1.Text = n3.ToString();
float result = n3 / n2;
answer.Text = result.ToString();
}
else if (sign.Text == "4")
{
float n1 = float.Parse(number1.Text);
float n2 = float.Parse(number2.Text);
float n3 = n1 * n2;
number1.Text = n3.ToString();
float result = n3 - n2;
answer.Text = result.ToString();
}
else {
MessageBox.Show("A critical error has occured");
}
If you walk through your old code, you will see the issue. Say you pull up an option 3 (should be divide). Your first if statement will evaluate true if sign.Text does not equal one. Since 3 does not equal one, the statement is true and the code inside the if statement is executed (doing a multiplication). Since we already found our value, we wouldn't need to evaluate the
else
statements.
A better way to do this code would be to use a
switch
statement instead. This is what they are designed for. I would also keep the
int
values as
int
for comparison purposes and only cast the value to string once when necessary. You also had some funky logic on option three and four. I changed that to be a simple operation. Finally, I changed your
rnd
function to be simpler. You didn't need all of those casts and conversions that you were doing. I've made these couple of changed below to show you what it would look like:
private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
{
int numberOne = rnd.Next(1, 10);
int numberTwo = rnd.Next(1, 10);
int operatorId = rnd.Next(1, 5);
number1.Text = numberOne.ToString();
number2.Text = numberTwo.ToString();
sign.Text = operatorId.ToString();
switch (operatorId)
case 1:
float result = numberOne * numberTwo;
answer.Text = result.ToString();
case 2:
float result = numberOne + numberTwo
answer.Text = result.ToString();
case 3:
float result = numberTwo / numberOne
answer.Text = result.ToString();
case 4:
float result = numberOne - numberTwo
answer.Text = result.ToString();
default:
MessageBox.Show("A critical error has occured");
}