为什么我的"测验"程序继续做两个选项正确的答案?

时间:2015-01-10 18:50:07

标签: c#

我首先要说明我对编程和堆栈溢出非常非常新。我试图找到我的问题的答案,但是从那以后他们都没有(或者他们没有为我工作)。 我目前正在学习c#,并且通过创建一个“测验”程序来测试我迄今为止学到的东西。我得到了它的工作,但后来注意到一个非常大的缺陷,每个问题有时可能有两个“准确”的答案(注意 - 这不应该是)。我发现我的问题是,当我的程序检查了所选答案时,它会选择一个新的随机问题并检查所选单选按钮是否是正确答案。希望有道理。 :)我一直想弄明白这一点,并决定是时候来这里了。 (我意识到它可能是重复的,但“重复”根本没有帮助我) 我做错了什么?

这是我到目前为止创建的代码:

namespace The_Q_and_A_Program
{
    public partial class QuestionForm : Form
    {
        public QuestionForm()
        {
            InitializeComponent();
            InitializeQuestions();
            GetQuestion();
            AssignValues();
            GetAnswer();
        }

        int NumQuestionList;
        public static Random RandomNum = new Random();
        public List<question> QuestionList = new List<question>();
        public string CurrentAnswer;
        public bool Op1IsTrue = false;
        public bool Op2IsTrue = false;
        public bool Op3IsTrue = false;
        public bool Op1Checked = false;
        public bool Op2Checked = false;
        public bool Op3Checked = false;
        public question RandomQuestion;

        public void InitializeQuestions()
        {
            question Q1 = new question("What was the only NFL team in history to play a 'Perfect Season', ending in a Superbowl win?", "Miami Dolphins", "New England Patriots", "Green Bay Packers", "Miami Dolphins");
            QuestionList.Add(Q1);
            question Q2 = new question("What NFL team won both Super Bowl I and Superbowl II?", "Denver Broncos", "Green Bay Packers", "New England Patriots", "Green Bay Packers");
            QuestionList.Add(Q2);
        }

        public void GetQuestion()
        {
            NumQuestionList = QuestionList.Count;
            RandomQuestion = QuestionList[RandomNum.Next(0, NumQuestionList)];
        }

        public void GetAnswer()
        {
            CurrentAnswer = RandomQuestion.Answer;

            if (CurrentAnswer == RandomQuestion.Op1)
            {
                Op1IsTrue = true;
            }

            else if (CurrentAnswer == RandomQuestion.Op2)
            {
                Op2IsTrue = true;
            }

            else if (CurrentAnswer == RandomQuestion.Op3)
            {
                Op3IsTrue = true;
            }
        }

        public void AssignValues()
        {
            QuestionLabel.Text = RandomQuestion.Question;
            Op1RadButton.Text = RandomQuestion.Op1;
            Op2RadButton.Text = RandomQuestion.Op2;
            Op3RadButton.Text = RandomQuestion.Op3;
        }

        public void CheckAnswer()
        {
            Op1Checked = Op1RadButton.Checked;
            Op2Checked = Op2RadButton.Checked;
            Op3Checked = Op3RadButton.Checked;

            if (Op1Checked == true & Op1IsTrue == true)
            {
                MessageBox.Show("Congratulations! You are correct!");
            }

            else if (Op2Checked == true & Op2IsTrue == true)
            {
                MessageBox.Show("Congratulations! You are correct!");
            }

            else if (Op3Checked == true & Op3IsTrue == true)
            {
                MessageBox.Show("Congratulations! You are correct!");
            }

            else
            {
                MessageBox.Show("Sorry, But that is incorrect.");
            }
        }

        private void CheckButton_Click(object sender, EventArgs e)
        {
            CheckAnswer();
        }

        private void NextButton_Click(object sender, EventArgs e)
        {
            GetQuestion();
            AssignValues();
            GetAnswer();
        }  
    }
}

这是“问题”类代码:

public class question
{
    public string Question;
    public string Op1;
    public string Op2;
    public string Op3;
    public string Answer;

    public question(string questionString, string op1, string op2, string op3, string answer)
    {
        Question = questionString;
        Op1 = op1;
        Op2 = op2;
        Op3 = op3;
        Answer = answer;
    }
}

我想另一种提出这个问题的方法是:我怎样才能使用RandomQuestion而不是每次都选择一个新的随机问题?

编辑:我开始提出问题的方式不正确(请参阅我与Jason Faulkner的评论以获取更多信息)。

编辑2:这个问题与其他问题的区别在于:我是愚蠢的,他们不是:)

1 个答案:

答案 0 :(得分:3)

  

如何在不每次选择新的随机问题的情况下使用RandomQuestion?

由于您在初始化程序时已经定义了问题,因此您只需在移动时从队列中删除所选/已显示的问题:

public void GetQuestion()
{
    NumQuestionList = QuestionList.Count;
    RandomQuestion = QuestionList[RandomNum.Next(0, NumQuestionList)];

    // Once question has been selected, remove it from the list.
    // This way it will not be selected again.
    QuestionList.Remove(RandomQuestion)
}

然后,您需要确保队列中仍有问题可用:

private void NextButton_Click(object sender, EventArgs e)
{
    if (QuestionList.Count == 0)
    {
        MessageBox.Show("No more questions.");
    }
    else
    {
        // Get next question.
        GetQuestion();
        AssignValues();
        GetAnswer();
    }
} 

请注意,Op[x]IsTrue变量是全局变量存在逻辑错误,但您永远不会重置它们。因此,基本上一旦设置为true,它将始终为真(这将导致错误的答案显示为正确)。你可以在这里纠正这个:

public void GetAnswer()
{
    // Reset answer flags.
    Op1IsTrue = false;
    Op2IsTrue = false;
    Op3IsTrue = false;

    CurrentAnswer = RandomQuestion.Answer;

    // Checks below will set the correct option.
    ...