0

我在验证用户输入时遇到问题,我有以下循环,它肯定会捕获它,但是当循环再次开始时,用户没有机会输入不同的值,所以值是相同的,只是创建了一个无限循环。

              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 尝试?

4

3 回答 3

1
int number;
if(string.IsNullOrEmpty(guessInput.Text))
{
  MessageBox.Show("You did not enter an integer, please enter a integer", "Invalid Values", MessageBoxButtons.OK, MessageBoxIcon.Error);
  return;
}
if(Int32.TryParse(guessInput.Text, out number))
{
  guessInt = number; 
}else
{
  MessageBox.Show("You did not enter an integer, please enter a integer", "Invalid Values",      MessageBoxButtons.OK, MessageBoxIcon.Error);
   guessInput.Text = "";
   return;
}


// when come to here you have guessInt, process it 

   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++;
于 2012-05-16T03:47:47.430 回答
1

看来你不需要一段时间:

          int number;

          if(guessInput.Text != "")
          {
              var 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 = "";
              }
           }

如果您还想验证空值,只需删除第一个 if:

          int number;

          var 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 = "";
          }
于 2012-05-16T03:32:51.203 回答
0

一旦用户在消息框上单击“确定”,循环将再次运行,而不给用户更改值的机会。

您需要做的是仅在他们输入猜测时运行验证。也就是说,与其有一个循环,不如有一些由事件(例如单击按钮)或由 winforms 提供的验证回调触发的代码。

这是我发现的关于使用验证回调的示例和简短文章:http: //blog.scosby.com/post/2010/02/11/Validation-in-Windows-Forms.aspx

在该示例中,请参见:

  • private void buttonSave_Click- 这是您放置消息框的地方,并且

  • private void textBoxNumber_Validating- 这是您放置pass = Int32.TryParse(guessInput.Text, out number)...代码的地方。

于 2012-05-16T03:36:47.530 回答