Do while循环导致编译失败

时间:2015-07-02 13:51:54

标签: c# do-while

我正在尝试用C#(Visual Studio 2013)制作一个简单的地理测验程序。我可以获得大部分内容,但是我需要检测用户何时选择了答案并按下“Enter”按钮。为此,我使用do-while循环等待用户在继续之前完成这两个要求,但是当添加do-wile循环时,它无法编译。失败,我的意思是编译从不给出错误并停止,或者做任何事情,只是坐在那里。我已经尝试注释掉do-while循环并且它编译得很好,所以错误必须与之相关。我错过了什么?

public partial class Form1 : Form
{
    int anselct = 0;
    bool buttonclick = false;
    bool runme = true;

    public Form1()
    {
        InitializeComponent();
        Q1.Text = Q1.Text.Replace("null", "Hello, welcome to the Geography quiz. Press 'A' and then 'Enter' to begin the quiz."); //replaces base text with introduction
        A1.Text = A1.Text.Replace("radioButton1", "A");
        A2.Text = A2.Text.Replace("radioButton3", "B");
        A3.Text = A3.Text.Replace("radioButton4", "C");
        A4.Text = A4.Text.Replace("radioButton2", "D");
        do {
            if (A1.Checked && buttonclick == true)
            {
                buttonclick = false;
                Q1.Text = Q1.Text.Replace("Hello, welcome to the Geography quiz. Press 'A' and then 'Enter' to begin the quiz.", "Question 1: What is the capital of Cuba"); //Changes text to that of the first question
                A1.Text = A1.Text.Replace("A", "Greenwich");
                A2.Text = A2.Text.Replace("B", "Berlin");
                A3.Text = A3.Text.Replace("C", "Bogota");
                A4.Text = A4.Text.Replace("D", "Havana");
                runme = false;
            } //end of if statement
       } while (runme == true); //end of do loop, should be infinate unless told to close
    }

//各种无关紧要的事情在这里

    public void Button_Click_1(object sender, EventArgs e)
    {
        buttonclick = true;
    }  //end of Button_Click_1
}

4 个答案:

答案 0 :(得分:2)

根据假设你的意思是'运行时'而不是'编译时',我将解决'挂'。

public void Button_Click_1(object sender, EventArgs e)
{
    if(A1.Checked)
    {   
       ReplaceText();
    }
}

private void ReplaceText()
{
    Q1.Text = Q1.Text.Replace("Hello, welcome to the Geography quiz. Press 'A' and then 'Enter' to begin the quiz.", "Question 1: What is the capital of Cuba"); //Changes text to that of the first question
    A1.Text = A1.Text.Replace("A", "Greenwich");
    A2.Text = A2.Text.Replace("B", "Berlin");
    A3.Text = A3.Text.Replace("C", "Bogota");
    A4.Text = A4.Text.Replace("D", "Havana");
}

<强>解释

编译时间是C#编译器构建代码的时间点。即,当它被调用/运行时,它会为它准备就绪。

运行时,您现在处于运行时

编译时错误往往发生在编译器发现语法错误时(通常还有其他情况)。

运行时错误(通常,一般来说,简而言之)发生在构建良好的代码中(语法上是合理的),但由于它已执行的路径或任何数量的环境变量而导致逻辑错误。

在这种情况下,事件是由于用户与UI交互而发生的事情。你已经成功连接了一个按钮事件处理程序,这很好,所以你应该触发一些功能。您不需要通过尽可能快速和频繁地轮询这些检查来实现这些检查。

悬挂问题

您的应用程序在运行循环时已“停止”。它必须在它移动到其余代码之前完成循环,但是你没有理由完成循环。所以它无限循环和循环。

答案 1 :(得分:0)

你需要消除循环,在构造函数中构建一个无限循环。

尝试使用您的事件形式,如Charles Mager在评论中写道。

您可以在此处找到有关活动的一些信息:

https://msdn.microsoft.com/en-us/library/system.windows.forms.form_events%28v=vs.110%29.aspx

答案 2 :(得分:0)

您当前的问题是构造函数中的无端循环,正如其他人已在评论中指出的那样,我建议您将程序更改为事件驱动的方式。如下所示:

namespace Photoviewer
{
    public partial class Form1 : Form
    {
        int anselct = 0;
        //bool buttonclick = false;
        //bool runme = true;


        public Form1()
        {
            InitializeComponent();
            Q1.Text = Q1.Text.Replace("null", "Hello, welcome to the Geography quiz. Press 'A' and then 'Enter' to begin the quiz."); //replaces base text with introduction
            A1.Text = A1.Text.Replace("radioButton1", "A");
            A2.Text = A2.Text.Replace("radioButton3", "B");
            A3.Text = A3.Text.Replace("radioButton4", "C");
            A4.Text = A4.Text.Replace("radioButton2", "D");

        }

//various irrelevant things are here

        public void Button_Click_1(object sender, EventArgs e)
        {
                if (A1.Checked)
                {                       
                    Q1.Text = Q1.Text.Replace("Hello, welcome to the Geography quiz. Press 'A' and then 'Enter' to begin the quiz.", "Question 1: What is the capital of Cuba"); //Changes text to that of the first question
                    A1.Text = A1.Text.Replace("A", "Greenwich");
                    A2.Text = A2.Text.Replace("B", "Berlin");
                    A3.Text = A3.Text.Replace("C", "Bogota");
                    A4.Text = A4.Text.Replace("D", "Havana");

                }
        }  //end of Button_Click_1
    }
}

答案 3 :(得分:0)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows;
using System.Windows.Input;



namespace Photoviewer
{
    public partial class Form1 : Form
    {
        int anselct = 0;
        bool runme = true;


        public Form1()
        {
            InitializeComponent();
            Q1.Text = Q1.Text.Replace("null", "Hello, welcome to the Geography quiz. Press 'A' and then 'Enter' to begin the quiz."); //replaces base text with introduction
            A1.Text = A1.Text.Replace("radioButton1", "A");
            A2.Text = A2.Text.Replace("radioButton3", "B");
            A3.Text = A3.Text.Replace("radioButton4", "C");
            A4.Text = A4.Text.Replace("radioButton2", "D");

        }

//various irrelevant things are here

        public void Button_Click_1(object sender, EventArgs e)
        {
            //do {
                if (A1.Checked)
                {
                    Q1.Text = Q1.Text.Replace("Hello, welcome to the Geography quiz. Press 'A' and then 'Enter' to begin the quiz.", "Question 1: What is the capital of Cuba"); //Changes text to that of the first question
                    A1.Text = A1.Text.Replace("A", "Greenwich");
                    A2.Text = A2.Text.Replace("B", "Berlin");
                    A3.Text = A3.Text.Replace("C", "Bogota");
                    A4.Text = A4.Text.Replace("D", "Havana");
                    //runme = false;
                } //end of if statement
           //} while (runme == true); //end of do loop, should be infinate unless told to close
        }  //end of Button_Click_1
    }
}

注释掉这些线条,因为只需按住按钮就可以继续循环。基本上就是这样:你想对事件的行为进行检查。不要坐在构造函数中并尝试挂起它以获取信息。这不是Winforms的工作方式。