gpt4 book ai didi

c# - 为什么我的 "Quiz"程序总是做出两个选项的正确答案?

转载 作者:行者123 更新时间:2023-11-30 22:01:14 26 4
gpt4 key购买 nike

<分区>

首先我想声明我对编程和堆栈溢出非常非常陌生。我试图找到我的问题的答案,但自那以后没有一个对我有用(或者对我不起作用)。我目前正在学习 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:这个问题与其他问题的区别在于:我很愚蠢,而他们不是:)

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com