我想先說明我對編程和堆棧溢出非常非常新。我試圖找到我的問題的答案,但他們沒有一個因爲我(或他們沒有爲我工作)。 我目前正在學習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:這個問題和其他人之間的區別是:我是愚蠢的,他們並沒有:)
我不明白你在問什麼。請嘗試更好地解釋。 – Phoenix
感謝調試:)沒有認識到這種語法錯誤(因爲感覺)。 – Ethan
你也可以使用Shuffle – Plutonix