代码之家  ›  专栏  ›  技术社区  ›  Kuiu

如何使循环等待,直到单击按钮

  •  0
  • Kuiu  · 技术社区  · 6 年前

    我正在做一个数学游戏,它会一个接一个地给出10个不同的加法问题。这些问题显示在标签上,您可以通过在文本框中写入并单击提交来回答。我一直在回答部分,尤其是在按下按钮之前循环等待。

    通过查找,我找到了一种将其作为新事件的方法,但我不知道如何使循环等待该事件继续

    我的代码如下所示

    int Between = 1;
            Random rnd = new Random();
            for (int i = 0; i < 10; i++)
            {
                if (Between == 1)
                {
                    int num1 = rnd.Next(1, 11); // 1-10
                    int num2 = rnd.Next(1, 11); // 1-10
                    string number1 = num1.ToString();
                    string number2 = num2.ToString();
                    kusimus.Text = number1 + " + " + number2;
                }
    

    我需要添加等待时间 kusimus.Text = number1 + " + " + number2; 。阅读文本框尚未添加,因为如果没有按钮,它将无法使用,因此它未包含在内。“between”不是完整的eiter,所以在它前面有if和int

    2 回复  |  直到 6 年前
        1
  •  1
  •   St. Pat    6 年前

    如果你想问总共10个问题,一次一个,你不需要使用循环并在其中等待。您只需使用按钮单击事件检查答案并更新问题标签。

    移动 Between rnd 成为类成员,因此可以通过多个方法访问它们。除此之外,创建两个整数来存储当前的正确答案,以及询问了多少问题。

    对于我的回答,我使用了以下名称:

    private int Between = 1;
    private Random rnd = new Random();
    private int questionsAsked = 0;
    private int currentAnswer = 0;
    

    更新表单构造函数中第一个问题的标签,如下所示。

    public Form1()
    {
        InitializeComponent();
    
        // Get two random numbers
        int num1 = rnd.Next(1, 11); // 1-10
        int num2 = rnd.Next(1, 11); // 1-10
    
        // Save the answer.
        currentAnswer = num1 + num2;
    
        // Update the label.
        kusimus.Text = String.Format("{0} + {1}", num1, num2);
    
        // Keep track of how many questions have been asked.
        questionsAsked++;
    }
    

    然后在click事件中执行许多相同的操作,包括答案检查。

    private void button1_Click(object sender, EventArgs e)
    {
        // We've already asked ten questions, don't do anything else.
        if (questionsAsked > 10) return;
    
        // If the user entered a valid integer into the text box
        int answer;
        if (int.TryParse(txtBoxAnswer.Text, out answer))
        {
            // Implement Between if still needed.
            if (Between == 1)
            {
                if (answer == currentAnswer)
                {
                    // the answer is correct.
                }
                else
                {
                    // the answer is incorrect
                }
    
                int num1 = rnd.Next(1, 11); // 1-10
                int num2 = rnd.Next(1, 11); // 1-10
    
                currentAnswer = num1 + num2;
    
                kusimus.Text = String.Format("{0} + {1}", num1, num2);
            }
    
            // We've asked another question.
            questionsAsked++;
    
            if (questionsAsked > 10)
            {
                // User has answered last question, do something?
            }
        }
    }
    
        2
  •  0
  •   Michael Puckett II    6 年前

    如果您将其放置在按钮单击事件中,它将在您单击按钮时工作并启动。

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        int between = 1;
        Random rnd = new Random();
    
        //This loop is pointless since there's only one number that can use it.
        //However; I've left it as it incase you're needing it for another reason.
        for (int i = 0; i < 10; i++)
        {
            if (between == 1)
            {
                int num1 = rnd.Next(1, 11); // 1-10
                int num2 = rnd.Next(1, 11); // 1-10
                string number1 = num1.ToString();
                string number2 = num2.ToString();
                kusimus.Text = number1 + " + " + number2;
            }
        }
    }
    

    现在,请注意以下几点,仅供参考:

    /* If the work is a loop, string manipulation, or anything that may require more than 100ms of work
    * then I suggest doing it asynchronously. Hopefully this helps.
    * If it's confusing or you need more comments to explain what's going on let me know.
    * Don't worry about the work being done... I just tried to keep it as similar as I could to your question
    * and still make it useful for the example. 
    * Note: This is WPF so the Textblock works like this but it should be RichTextBox for WinForms and button will just be button.Enabled = true : false */
    private async void Button_Click(object sender, RoutedEventArgs e)
    {
        button1.IsEnabled = false;
        textblock1.Text = string.Empty;
    
        var between = 1;
        Random rnd = new Random();
    
        var randomText = await Task.Run(() =>
        {
            var stringBuilder = new StringBuilder();
            for (int i = 0; i < 1000; i++)
            {
                if (between == 1)
                {
                    int num1 = rnd.Next(1, 11); // 1-10
                    int num2 = rnd.Next(1, 11); // 1-10
                    string number1 = num1.ToString();
                    string number2 = num2.ToString();
                    stringBuilder.AppendLine(number1 + " + " + number2);
                }
            }
            return stringBuilder.ToString();
        });
    
        textblock1.Text = randomText;
        button1.IsEnabled = true;
    }