1

I'm confused on how to get this program to do a few certain things and a question for future reference for myself:

Question: Can you do if statements with buttons example: if (button1 is clicked) do this else if (button2 is clicked) do this

The goal of my program is to help learn basic integer mathematics and the four sections are ADDING, SUBTRACTING, MULTIPLYING, and MOD. I have four buttons for each one of those topics, and a fifth button to submit the answer for the question, I have 3 text boxes, the first being the question that is presented, second being the users answer, third being whether the answer was correct or not

What I currently have set up:

        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!";



    }
}

}

and what I'm looking to do is if button 1 is clicked you add, button 2 you subtract, button 3 you multiply, button 4 you mod and then depending on which one was clicked you click submit and it'll tell you whether you got the right answer or not. What I currently have somewhat does that but if the answer is any of the four types of questions answers it'll show it as correct.

ie: the question is 8 + 3 and you put 5, it'll say correct because 8 - 3 is 5

user3029872
  • 11
  • 1
  • 2
  • 1
    `the question is 8 + 3` -> I thought if user entered `5`, it would say `Wrong!` because the answer should be `11`? – King King Nov 25 '13 at 03:31
  • It should say its wrong but because my if statement is flawed but I dont know how to fix it, it should say wrong but because the if statement includes if its the answer of the 8 - 3 which is 5, itll say correct – user3029872 Nov 25 '13 at 03:32
  • Side note: your `Random` usage is completely random. Please check out [Random number generator only generating one random number](http://stackoverflow.com/questions/767999/random-number-generator-only-generating-one-random-number) for proper usage. – Alexei Levenkov Nov 25 '13 at 03:37

4 Answers4

2

You can try the following code instead, you code has fairly many things redundant and the checking mechanism to determine if user answers correctly is totally wrong:

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!");
   }
}
King King
  • 61,710
  • 16
  • 105
  • 130
  • 1
    +1. Nice touch with `TryParse`. One can use `Dictionary>>` to make `switch` more generic, generate all handlers in a loop to make code cooler. – Alexei Levenkov Nov 25 '13 at 03:59
  • 1
    @AlexeiLevenkov thanks, I know of the `delegate` use, but this is better for a newbie I think, even we don't need each event handler for each button, I would like to keep it simple for the OP. – King King Nov 25 '13 at 04:02
0

You can assign one event handler to multiple buttons, and then check the EventArgs to tell which button fired the event, and put appropriate conditional logic in there.

Not sure if this is ASP.NET or WinForms, but it works something along the lines of:

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
    }
 }

I have added the event handlers declaratively but you could do this through the properties dialog or in the markup.

codemonkeh
  • 2,054
  • 1
  • 20
  • 36
0

You are generating random (*) numbers to create question and again to validate it. It makes absolutely no sense since you have something like "What is 3 + 4" and you comparing result to 7 + 2 or some other random value.

You should save generated values and operation for verification in some member variable, or even easier simply save expected result.

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

(*) Note that your current "random" numbers are not exactly random due to your code restarting sequence on each call - so pairs of numbers would be very closely related. Note that numbers generated for expression will likely be different from numbers generated for the check. Proper usage is to have one single Random object (potentially one-per-thread, but you don't need to worry about it for your case).

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • -1 Their question is not related to random number generation (although I agree there is room for improvement there). – codemonkeh Nov 25 '13 at 03:42
  • @codemonkeh this answers the question and also provide more elegant solution, (1+ Alexei) – Damith Nov 25 '13 at 03:49
  • @codemonkeh - while OP problem is not related to if random numbers generated correctly or not, it partially is caused by the fact that numbers used for problem and solution are random (important part is that they are not related to each other - could have been simply hard-coded to be different with the same result). – Alexei Levenkov Nov 25 '13 at 03:55
  • @AlexeiLevenkov I think the important point is to answer their first and fore most. Additional suggestions are valuable but not the priority. – codemonkeh Nov 25 '13 at 04:11
  • @codemonkeh - To me the question boils down to "why comparing user input with random number produce random results". You seem to have another interpretation based on your answer, but I'm not sure what your understanding is. – Alexei Levenkov Nov 25 '13 at 04:16
  • @AlexeiLevenkov Hmmn my interpretation was that the OP simply wants to combine his event handlers into a single method. I'll be sorry if I'm wrong ;) – codemonkeh Nov 25 '13 at 04:27
0

I am assuming you are doing a Windows Application here (not Web). In that case, why don't you simply have a class level variable to store the last operation (subtract, add etc), and include check of that variable in the if condition.

eg:

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!";
Tharaka
  • 2,355
  • 1
  • 20
  • 22