I'd like to start by stating that I am very, very new to programming and to stack overflow. I've tried to find an answer to my question, but none of them made since to me (or they didn't work for me). I am currently learning c#, and am testing what I've learned thus far by creating a "quiz" program. I got it to working, but then noticed a very large flaw, each question could sometimes have two "accurate" answers (note - this is not supposed to be). I found that my problem was that when my program checked the selected answer, it would pick a new random question and check if the selected radio button was the correct answer. Hope that made sense. :) I've been trying to figure this out, and decided it was time to come here. (I realize it is probably a duplicate, but the "duplicates" didn't help me at all) What am I doing wrong?
Here is the code I've created thus far:
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();
}
}
}
Here is the "question" class code:
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;
}
}
I guess another way to put this question is: How can I use the RandomQuestion without picking a new random question every time?
Edit: The way I stated my question to begin with was incorrect (refer to my comments with Jason Faulkner for more info).
Edit 2: The difference between this question and others is: I was stupid and they weren't :)