1
    private void QuestionAnswer_Load(object sender, EventArgs e)
    {

        txtQuestion.Enabled = false;
        txtQuestion.BackColor = Color.White;
        grpMultipleChoice.Enabled = false;
        grpSingleChoice.Enabled = false;

        btnCheckAnswer.Enabled = false;
        btnNext.Enabled = false;
        btnQuit.Enabled = false;

        //force student to enter registration details
        if (txtStudentName.Text == "" && txtStudentNumber.Text == "" && txtModuleNumber.Text == "")
        {
            btnStart.Enabled = false;
            MessageBox.Show("You must enter your registration details at the upper right corner");
        }

        //declare a list
        lstNumbers = new ArrayList();

        //create a random number generator
        Random rndNumber = new Random();

        //generate 70 random numbers
        //int number = (int)(rndNumber.NextDouble() * 69) + 1;

        //lstNumbers.Add(number);
        ///use this counter to loop whenever a number is generated
        int count = 0;
        int maximumNumber = 69;

        ///disable answer button 
        btnCheckAnswer.Enabled = false;
        do
        {

            int number = (int)(rndNumber.NextDouble() * maximumNumber) + 1;
            lstNumbers.Add(number);
            if (!lstNumbers.Contains(number))
            {
                lstNumbers.Add(number);
            }
            count++;

        } while (count <= 15 * 70);//
        btnCheckAnswer_Click(sender, e);
        return lstNumbers;//returns once list is built
     }

说明:我创建了一个数组列表,允许我在 1-70 之间随机生成,但注意到我的数字有些重复。为了防止这种情况,我注意到我在下面收到一条错误消息:

错误 1 ​​由于 'wwTestAppV1.GenKnow.QuestionAnswer_Load(object, System.EventArgs)'返回 void,return 关键字后面不能跟对象表达式

我已尽我所能解决这个问题。当我删除此语句但显示重复值时,程序运行良好。能否请你帮忙?

4

1 回答 1

8

您的方法签名是:

private void QuestionAnswer_Load(object sender, EventArgs e)

返回类型void意味着您不能/不打算返回任何东西。但是,在底部,您有:

return lstNumbers;//returns once list is built

你要回东西了!编译器抱怨您告诉它的指令有冲突。将返回类型更改为ArrayList或不返回任何内容。

话虽如此,可以对代码进行一些改进以使其更具可读性,这也将帮助您破译问题。考虑将您的代码提交到https://codereview.stackexchange.com/

于 2012-10-11T15:54:19.870 回答