程序总是说你输入的任何数字的答案都是正确的。键入字符时输出不正确。我不确定为什么会这样做。我是C#的新手,所以放轻松!
// Create a Random object to generate random numbers.
Random randomizer = new Random();
// for the addition problem.
int num1;
int num2;
int sum;
int correct;
int incorrect;
private void play_Load(object sender, EventArgs e)
{
StartTheGame();
}
public void StartTheGame()
{
num1 = randomizer.Next(11);
num2 = randomizer.Next(11);
sum = num1 + num2;
labelNum1.Text = num1.ToString();
labelNum2.Text = num2.ToString();
}
private void labelNum1_Click(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
}
private void btnSub_Click(object sender, EventArgs e)
{
if (int.TryParse(txtUserAnswer.Text, out sum))
{
MessageBox.Show("Correct Answer!", "Correct");
correct++;
}
else
{
MessageBox.Show("You messed up.", "Incorrect");
incorrect++;
}
txtUserAnswer.Clear();
txtUserAnswer.Focus();
StartTheGame();
if(correct == 5)
{
MessageBox.Show("Congrats! You defeated the monster.", "Next Level");
}
if(incorrect == 3)
{
MessageBox.Show("You've been defeated. Nerd.", "Loser");
Application.Exit();
}
}
请帮帮我!这是为学校,这困扰我!
答案 0 :(得分:2)
你在这里做的只是尝试解析。只要.mask( [msg], [msgCls] )
Masks this component with a semi-opaque layer and makes the contents unavailable to clicks.
Parameters
•msg : String (optional)
A message to show in the center of the mask layer.
•msgCls : String (optional)
A CSS class name to use on the message element in the center of the layer.
的输入为整数,此条件始终为int.TryParse(txtUserAnswer.Text, out sum)
。
true
您必须使用与txtUserAnswer.Text = "1235"; //this result in true
txtUserAnswer.Text = "44"; //this result in true
txtUserAnswer.Text = "999999"; //this result in true
txtUserAnswer.Text = "123bbn"; //this result in false
不同的变量名添加检查条件。否则,您的sum
将被用户的输入替换:
sum