2

enter image description hereI am creating a C# application that generates two random integers,between 100 to 500. The numbers should perform addition such that

           247 + 129 = ?

The form has a text box for the user to enter the problem's answer. When a button is clicked, the application should do the following:

Check the user's input and display a message indicating whether it is the correct answer or not. Generate two new random numbers and display them in a new problem on the form add a button named "Save score to file".

When clicked, this button should write the total number of problems, the number of correct answers as well as the percentage of problems answered correctly.

Code:

InitializeComponent();

        Random rand = new Random();
        {
            int number1;

            number1 = rand.Next(400) + 100;

            numberLabel1.Text = Convert.ToString(number1);
        }
        {

            int number2;

            number2 = rand.Next(400) + 100;
            numberLabel2.Text = Convert.ToString(number2);
        }
    }

    private void checkButton_Click(object sender, EventArgs e)
    {

        int correctAnswer;
        correctAnswer = int.Parse(numberLabel1.Text) + int.Parse(numberLabel2.Text);
        int userAnswer;
        userAnswer = Convert.ToInt32(userInputBox.Text);

        if (userAnswer == correctAnswer)
        {
            MessageBox.Show("Your Answer is Correct");
        }
        else
        {
            MessageBox.Show("Your Answer is Incorrect");
        }
    }
    private void clearButton_Click(object sender, EventArgs e)
    {

        numberLabel1.Text = "";
        numberLabel2.Text = "";
        userInputBox.Text = "";
    }

    private void exitButton_Click(object sender, EventArgs e)
    {

        this.Close();
    }

    private void answerBox_TextChanged(object sender, EventArgs e)
    {

               }
}

}

The question I have is: How do I get an output? The message box isn't showing and I answer the problem correctly each time. After This how do Generate two new random numbers and display them in a new problem on the form add a button named "Save score to file".

When clicked, this button should write the total number of problems, the number of correct answers as well as the percentage of problems answered correctly.

  • Your question is quite large...could you try reducing it a bit to the specific thing you are having trouble with? If you're having trouble with more than one thing, it's easier for us to help if you ask a separate question for each thing. – Richard Ev Oct 05 '18 at 04:21
  • Have you tried [setting a breakpoint](https://learn.microsoft.com/en-us/visualstudio/debugger/using-breakpoints?view=vs-2017) in the `checkButton_Click` method, to verify that it is getting called? – Richard Ev Oct 05 '18 at 04:27
  • So is your actual issue that you can't get a message box to display when a button gets clicked? How about trying to get just this to work in a separate, new, application? – Richard Ev Oct 05 '18 at 04:37
  • OK, let's look at those one-at-a-time... 1) `MessageBox.Show("testing");` in an otherwise empty "click" event handler for a button will cause a message box to be displayed. If not, something strange is happening on your machine. 2) To generate multiple random numbers, see [this existing answer](https://stackoverflow.com/questions/2706500/how-do-i-generate-a-random-int-number-in-c). Note that you don't need the extra { braces } around your random number code. 3) Are you having trouble showing this, or calculating it? – Richard Ev Oct 05 '18 at 05:12
  • (If you can wait a few hours, I'll see about adding a useful answer later on tonight) – Richard Ev Oct 05 '18 at 05:23
  • Looks like you have quite a few answers now...have they helped? – Richard Ev Oct 05 '18 at 12:07

2 Answers2

0
private static Random rand = new Random();
private void checkButton_Click(object sender, EventArgs e)
{
    int num1 = rand.Next(400) + 100;
    int num2 = rand.Next(400) + 100;
    label1.Text = num1.ToString();
    label2.Text = num2.ToString();
    int correctAnswer = num1 + num2;
    int userAnswer = Convert.ToInt32(textBox1.Text);

        if (userAnswer == correctAnswer)
        {
            MessageBox.Show("Your Answer is Correct");
        }
        else
        {
            MessageBox.Show("Your Answer is Incorrect");
        }
}
  • Hi and welcome to Stack Overflow. Please try to provide an explanation when submitting code as an answer. See [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) for more information. – Ruzihm Oct 05 '18 at 07:04
-1

[First]

Console.WriteLine ( String.Format("Answer => " + userAnswer ) );

will show it on console window

MessgeBox.Show( ( String.Format("Answer => {0}", userAnswer ) );

will show it on MessageBox.

I put 2 types of how to use String.Format for you :)

[Second]

you can make a button which do the task again.

put your generating code under the button function

[Third]

You need to study about StreamWriter

S M
  • 3,133
  • 5
  • 30
  • 59
Arphile
  • 841
  • 6
  • 18
  • A minor point - in the first example, you don't need to use `string.Format`, because `"Answer => " + userAnswer` will do the job of creating the string itself. – Richard Ev Oct 05 '18 at 04:25