数学测验问题 [英] Problem with Maths Quiz

查看:60
本文介绍了数学测验问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我目前正在进行数学测验,以测试心算.我基本上会生成两个介于1到10之间的随机数,然后用户必须对这两个数进行加,减,乘或除.我试图通过生成一个介于1和4之间的数字来做到这一点.1表示用户必须相乘,2表示用户必须相加,3表示用户必须相除,4表示用户必须相减.但是,每当我尝试使计算机除以或减去时,它只会乘以乘积,否则一切都会正常进行.我敢肯定问题很明显,但我看不出它是什么.非常感谢您的帮助.

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.

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语句都在检查值是否等于指定的数字.将每个更改为相等,您应该有一个可行的解决方案.它应该是这样的:

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");
  }


如果您遍历旧代码,则会看到此问题.假设您拉出一个选项3(应该除以3).如果sign.Text不等于1,则您的第一个if语句将评估为true.由于3不等于1,因此该语句为true,并且执行if语句中的代码(执行乘法).由于我们已经找到了自己的价值,因此无需评估else语句.

执行此代码的更好方法是使用switch语句.这就是他们的目的.为了比较起见,我还将int的值保留为int,并且仅在必要时将值强制转换为字符串一次.您还对选项三和四有一些时髦的逻辑.我将其更改为简单的操作.最后,我将您的rnd函数更改为更简单.您不需要所有正在执行的转换和转换.我在下面做了几处更改,以向您展示它的外观:


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");
  }


这篇关于数学测验问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆