为什么我的“测验”程序不断做出两个正确答案的选项

本文关键字:两个 选项 答案 测验 我的 程序 为什么 | 更新日期: 2023-09-27 18:37:06

>我想首先声明我对编程和堆栈溢出非常非常陌生。我试图找到我问题的答案,但从那以后他们都没有给我(或者它们对我不起作用)。我目前正在学习 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?

编辑:我一开始陈述问题的方式是不正确的(有关更多信息,请参阅我对杰森福克纳的评论)。

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

为什么我的“测验”程序不断做出两个正确答案的选项

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

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

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.
    ...