jeudi 5 octobre 2017

Addition Tutor Program doesnt work properly ***read description***

Below is the code for a program called "Addition Tutor". The program generates 2 random numbers and requests the user to answer the addition of both the random numbers. The problem is that every time I answer the question correctly, the messagebox returns the else action ("Incorrect"). What's the Problem with my program? Please keep the answers simple, my programing knowledge and terminology is very limited. Your help is greatly appreciated!

namespace Addition_Tutor
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            //Create a random object.
            Random rand = new Random();
            {
                //Declare an Integer
                int number1;

                //Generate a random integer and assign it to number1
                number1 = rand.Next(400) + 100;

                //Send random number to numberlabel1
                numberLabel1.Text = Convert.ToString(number1);
            }
            {
                //Declare an Int Variable
                int number2;

                //Generate a random integer and assign it to number2
                number2 = rand.Next(400) + 100;

                //Send random number to numberlabel2
                numberLabel2.Text = Convert.ToString(number2);
            }
        }

        private void checkButton_Click(object sender, EventArgs e)
        {

                //declare an Int Variable for Correct Answer
                int correctAnswer;
                correctAnswer = Convert.ToInt32("" + numberLabel1.Text + numberLabel2.Text);

            //check to see if the userAnswer and correctAnswer match.
            int userAnswer;
            userAnswer = Convert.ToInt32(answerTextBox.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)
        {
            //clear the labels and textboxes.
            numberLabel1.Text = "";
            numberLabel2.Text = "";
            answerTextBox.Text = "";
        }

        private void exitButton_Click(object sender, EventArgs e)
        {
            //close the form
            this.Close();
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire