Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hi I am currently working on a maths quiz that tests mental arithmetic. I basically generate two random numbers between 1 and 10 and the user then had to add, subtract, multiply or divide the two numbers. I have tried to do this by generating a number between 1 and 4. 1 means the user has to multiply, 2 means the user had to add, 3 means the user has to divide and 4 means the user has to subtract. However whenever I try to get the computer to divide or subtract it just multiplies instead, otherwise everything works as it should. I am sure that the problem is very obvious but I can't see what it is. Any help is much appreciated.

C#
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");
          }
Posted
Updated 10-Jul-12 10:14am
v2
Comments
lewax00 10-Jul-12 16:31pm    
I think you're over doing some things here...
rnd.Next(Convert.ToInt32(1), Convert.ToInt32(10)).ToString()
can just be
rnd.Next(1, 10).ToString()

Also, what's up with the extra steps in division and subtraction? Your division looks like it will always return n1 (because n3 / n2 = (n1 * n2) / n2 = n1).
Alex Foster 10-Jul-12 16:43pm    
The extra steps are there in the division and subtraction because I need the answers to be fairly straightforward as the app I am building is aimed at kids. It would be pretty bad to ask a kid to divide 4 by 9 in their head. I am currently working on a better way though.
Thanks for your other suggestions I have now implemented them.
Sergey Alexandrovich Kryukov 10-Jul-12 16:59pm    
Yet another method to train trained monkeys. More monkeys able to do the "mental math", less people who can think.
--SA

1 solution

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:

C#
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:
C#
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");
  }
 
Share this answer
 
v3
Comments
[no name] 10-Jul-12 16:24pm    
I think you are right
Alex Foster 10-Jul-12 16:30pm    
Thanks very much, works well now.
Tim Corey 10-Jul-12 16:35pm    
I made some updates to my solution just a minute ago as a type of mini code review. Please look it over. I think some of these should help. The issues on option three and four might be ok for your environment but I made the changes to the way I thought they should be. Make sure you test it before just making the changes live.
Alex Foster 10-Jul-12 16:50pm    
The extra steps are there in the division and subtraction because I need the answers to be fairly straightforward as the app I am building is aimed at kids. It would be pretty bad to ask a kid to divide 4 by 9 in their head. I am currently working on a better way though.
Thanks for your other suggestions I have now implemented them.
SoMad 10-Jul-12 17:19pm    
Nice answer and solved OP's question - my +5.

Soren Madsen

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900