因此,我需要製作一個應用程序,它在2個標籤中生成2個數字,然後用戶必須輸入這兩個數字的附加值作爲答案。 然後,我需要做一個按鈕,檢查用戶輸入正確的解決方案在文本框中...錯誤的驗證按鈕?
但是,我做的檢查按鈕,顯示答案每次「不正確」...我在做什麼錯誤?
private void generatebutton_Click(object sender, EventArgs e
{
//Declaring Variables for Generate button
int answer = 0;
Random rand = new Random();
var intNum1 = rand.Next(100, 500);
var intNum2 = rand.Next(100, 500);
answer = intNum1 + intNum2;
//Generate integer number 1 in first label
firstnumberlabel.Text = intNum1.ToString();
//Generate integer number 2 in second label
secondNumberlabel.Text = intNum2.ToString();
}
private void checkButton_Click(object sender, EventArgs e)
{
//Declaring variables for answer label
int userInput = 0;
int answer = 0;
Random rand = new Random();
var intNum1 = rand.Next(100, 500);
var intNum2 = rand.Next(100, 500);
answer = intNum1 + intNum2;
if (int.TryParse(txtanswer.Text, out userInput))
{
if (userInput == answer)
{
// Display success message
MessageBox.Show("Correct!");
}
else
{
// Display wrong answer message
MessageBox.Show("Incorrect!");
}
}
else
{
// show message that invalid input
}
}
private void exitButton_Click(object sender, EventArgs e)
{
//Closes Application
this.Close();
}
private void button4_Click(object sender, EventArgs e)
{
//Clears text box
txtanswer.Text = "";
}
}
}
這是**沒有必要**重複標題中的標籤信息。我刪除它是有原因的。做**不**再添加它。這裏的標籤系統工作得非常好,它不需要你的幫助。 –