我在验证用户输入时遇到问题,我有以下循环,它肯定会捕获它,但是当循环再次开始时,用户没有机会输入不同的值,所以值是相同的,只是创建了一个无限循环。
private void guess_Click(object sender, EventArgs e)
{
int guessInt = 0;
bool pass = false;
int number;
while (pass == false)
{
if (guessInput.Text != "")
{
pass = Int32.TryParse(guessInput.Text, out number);
if (pass)
{
guessInt = number;
}
else
{
MessageBox.Show("You did not enter an integer, please enter a integer", "Invalid Values", MessageBoxButtons.OK, MessageBoxIcon.Error);
guessInput.Text = "";
}
}
else MessageBox.Show("You did not enter anything, please enter a integer", "Invalid Values", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
guess.Enabled = false;
next_Guess.Enabled = true;
if (guessInt == randomArray[x])
{
result.Text = ("You Win! The correct number was " + randomArray[x]);
right += 1;
correctAnswers.Text = right.ToString();
}
else
{
result.Text = ("Sorry you lose, the number is " + randomArray[x]);
wrong += 1;
incorrectAnswers.Text = wrong.ToString();
}
hintLabel.Enabled = false;
x++;
}
那么用户如何有机会重新输入一个值并重新开始循环,或者我应该在这里使用 try/catch 尝试?