随机数和多个按钮c#

时间:2013-11-25 03:28:16

标签: c# button if-statement numbers statements

我很困惑如何让这个程序做一些特定的事情和一个问题供我自己参考:

问题: 你能用按钮做if语句吗? 示例:if(单击button1)             做这个          否则if(单击button2)             这样做

我的程序的目标是帮助学习基本的整数数学,这四个部分是ADDING,SUBTRACTING,MULTIPLYING和MOD。 我有四个按钮用于每个主题,第五个按钮提交问题的答案,我有3个文本框,第一个是提出的问题,第二个是用户答案,第三个是答案是否是是否正确

我目前的设置:

        private void button1_Click(object sender, EventArgs e)
    {
        Random random1 = new Random();
        int randomNumber1 = random1.Next(6, 11);
        Random random2 = new Random();
        int randomNumber2 = random2.Next(1, 6);
        textBox1.Text = "What is " + randomNumber1 + " + " + randomNumber2 + " ?";
    }

    private void button2_Click(object sender, EventArgs e)
    {
        Random random1 = new Random();
        int randomNumber1 = random1.Next(6, 11);
        Random random2 = new Random();
        int randomNumber2 = random2.Next(1, 6);
        textBox1.Text = "What is " + randomNumber1 + " - " + randomNumber2 + " ?";
    }

    private void button3_Click(object sender, EventArgs e)
    {
        Random random1 = new Random();
        int randomNumber1 = random1.Next(6, 11);
        Random random2 = new Random();
        int randomNumber2 = random2.Next(1, 6);
        textBox1.Text = "What is " + randomNumber1 + " * " + randomNumber2 + " ?";
    }

    private void button4_Click(object sender, EventArgs e)
    {
        Random random1 = new Random();
        int randomNumber1 = random1.Next(6, 11);
        Random random2 = new Random();
        int randomNumber2 = random2.Next(1, 6);
        textBox1.Text = "What is " + randomNumber1 + " % " + randomNumber2 + " ?";
    }

    private void button5_Click(object sender, EventArgs e)
    {
        Random random1 = new Random();
        int randomNumber1 = random1.Next(6, 11);
        Random random2 = new Random();
        int randomNumber2 = random2.Next(1, 6);




        if (Convert.ToInt16(textBox2.Text) == (randomNumber1) + (randomNumber2))
            textBox3.Text = "Correct!";
        else if (Convert.ToInt16(textBox2.Text) == (randomNumber1) - (randomNumber2))
            textBox3.Text = "Correct!";
        else if (Convert.ToInt16(textBox2.Text) == (randomNumber1) * (randomNumber2))
            textBox3.Text = "Correct!";
        else if (Convert.ToInt16(textBox2.Text) == (randomNumber1) % (randomNumber2))
            textBox3.Text = "Correct!";
        else
            textBox3.Text = "Incorrect!";



    }
}

}

我要做的是,如果你点击按钮1,你减去按钮2,按钮3你乘,按钮4你mod,然后根据点击哪一个你点击提交它会告诉你是否得到了正确的答案。我目前在某种程度上做了什么,但如果答案是四种类型的问题答案中的任何一种,它将表明它是正确的。

ie:问题是8 + 3而你放5,它会说正确因为8 - 3是5

4 个答案:

答案 0 :(得分:2)

您可以尝试使用以下代码,代码中包含相当多的冗余内容,并且用于确定用户是否正确回答的检查机制完全错误:

public enum Operation {
  Add,
  Subtract,
  Divide,
  Multiply,
  Modulo
}
Random rand = new Random();
private decimal GenerateQuestion(Operation o){
  int a = rand.Next(6, 11);
  int b = rand.Next(1, 6);
  decimal result = 0;
  string os = "";
  switch(o){
    case Operation.Add:          
         result = a + b;
         os = "+";
         break;
    case Operation.Subtract:
         result = a - b;
         os = "-";
         break;
    case Operation.Multiply: 
         result = a * b;
         os = "*";
         break;
    case Operation.Divide:
         result = (decimal)a/b;
         os = "/";
         break;
    case Operation.Modulo:
         result = a % b;
         os = "%";
         break;
  }
  textBox1.Text = string.Format("What is {0} {1} {2}?", a,os,b);
  return result;
}
decimal result;    
private void button1_Click(object sender, EventArgs e)
{
   result = GenerateQuestion(Operation.Add);
}
private void button2_Click(object sender, EventArgs e){
   result = GenerateQuestion(Operation.Subtract);
}
private void button3_Click(object sender, EventArgs e){
   result = GenerateQuestion(Operation.Multiply);
}
private void button4_Click(object sender, EventArgs e){
   result = GenerateQuestion(Operation.Modulo); 
}
private void button5_Click(object sender, EventArgs e){
   decimal v;
   if(decimal.TryParse(textBox2.Text, out v)){
     textBox3.Text = (v == result) ? "Correct!" : "Incorrect!";
   }else {
     textBox3.Clear();
     MessageBox.Show("Enter a number please!");
   }
}

答案 1 :(得分:0)

您可以为多个按钮分配一个事件处理程序,然后检查EventArgs以告知触发事件的按钮,并在其中放置适当的条件逻辑。

不确定这是ASP.NET还是WinForms,但它的工作原理如下:

btn1.Click += button_Click;
btn2.Click += button_Click;
btn3.Click += button_Click;

 private void button_Click(object sender, EventArgs e)
 {
    var btn = sender as Button;
    if (sender != null) {
        if (btn is btn1) {
            //do something
        }
        else if (btn is btn2) {
            //do something
        }
        //etc
    }
 }

我以声明方式添加了事件处理程序,但您可以通过属性对话框或标记来执行此操作。

答案 2 :(得分:0)

您正在生成随机(*)数字以创建问题,然后再次验证它。这完全没有意义,因为你有“什么是3 + 4”之类的东西,你将结果与7 + 2或其他随机值进行比较。

您应该在某些成员变量中保存生成的值和验证操作,或者更简单地保存预期结果。

Random random = new Random();
int expectedResult;
private void button3_Click(object sender, EventArgs e)
{
    int randomNumber1 = random.Next(6, 11);
    int randomNumber2 = random.Next(1, 6);
    textBox1.Text = "What is " + randomNumber1 + " * " + randomNumber2 + " ?";
    expectedResult = randomNumber1 * randomNumber2;
}

private void button5_Click(object sender, EventArgs e)
{  
   textBox3.Text = 
     int.Parse(textBox2.Text) == expectedResult ? "Correct!" : "Incorrect!";
}

(*)请注意,由于每次调用时代码重新启动序列,您当前的“随机”数字并不是完全随机的 - 因此数字对将非常密切相关。请注意,为表达式生成的数字可能与为检查生成的数字不同。正确的用法是拥有一个Random个对象(可能是每个线程一个,但你不需要为你的情况担心)。

答案 3 :(得分:0)

我假设你在这里做一个Windows应用程序(而不是Web)。在这种情况下,为什么不简单地使用类级变量来存储最后一个操作(减去,添加等),并在if条件中包含对该变量的检查。

例如:

public enum Operations
{
   Add,
   Subtract
}

   private void button1_Click(object sender, EventArgs e)
    {
        Random random1 = new Random();
        int randomNumber1 = random1.Next(6, 11);
        Random random2 = new Random();
        int randomNumber2 = random2.Next(1, 6);
        var currentOperation = Operations.Add
        textBox1.Text = "What is " + randomNumber1 + " + " + randomNumber2 + " ?";
    }

if (Convert.ToInt16(textBox2.Text) == (randomNumber1) + (randomNumber2) && currentOperation == Operations.Add)
            textBox3.Text = "Correct!";