Reputation: 57
I have been trying to make my first app, A quiz app, and can't seem to get my head around how I would validate a correct answer when the appropriate button is clicked to then move on to the next question.
Quiz Page:
public partial class Page1 : PhoneApplicationPage
{
// list array
List<Question> qu;
//counter for moving to next question
int questionNumber;
// correct answer counter
int correctAnswer;
public void main()
{
// method for adding more questions to the list
qu = new List<Question>();
qu.Add(new Question("what is your favourite colour?",
"blue",
"red",
"green",
"blue"));
qu.Add(new Question("what is your favourite film?",
"The Matrix",
"Star Wars",
"Wrath Of Khan",
"The Matrix"));
qu.Add(new Question("what is your favourite car?",
"BMW",
"VW",
"Mercedes",
"BMW"));
questionNumber = 0;
correctAnswer = 0;
}
public Page1()
{
InitializeComponent();
main();
// counter for displaying next question
displayQuestion(questionNumber);
}
// button for quitting back to the start screen
private void btn_quit_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(
new Uri("/MainPage.xaml", UriKind.Relative));
}
private void btn_Answer_A_Click(object sender, RoutedEventArgs e)
{
endQuestion();
}
private void btn_Answer_C_Click(object sender, RoutedEventArgs e)
{
endQuestion();
}
private void btn_Answer_B_Click(object sender, RoutedEventArgs e)
{
endQuestion();
}
// method for ending a question, inc : counter for
// moving on to next question and
// checking if the answer is correct or not
public void endQuestion()
{
//check the answer
//if it's correct
questionNumber++;
displayQuestion(questionNumber);
//otherwise
//stay on question
}
public void displayQuestion(int counter)
{
// where the question and answers are displayed
// in the buttons and txt block
txt_block_question.Text = counter + ". " + qu[counter].question;
btn_Answer_A.Content = qu[counter].a;
btn_Answer_B.Content = qu[counter].b;
btn_Answer_C.Content = qu[counter].c;
}
}
Question Class
public class Question
{
public String question;
public String answer;
public String a;
public String b;
public String c;
public Question(string q, string an, string optionA, string optionB, string optionC)
{
question = q;
answer = an;
a = optionA;
b = optionB;
c = optionC;
}
}
Upvotes: 0
Views: 1431
Reputation: 8037
Based on the code you've displayed, I think the solution is fairly simple:
First, you will want to pass a variable to endQuestion()
, telling it which answer was chosen. Since each answer button has the text of the answer stored in its Content
value, you can just pass that.
Second, you will want to update endQuestion()
to take an "answer" parameter and compare that answer to the correct answer stored in your Question
variable (qu[counter].answer
). Using String.Compare(string, string) would be a good way to perform the comparison.
This should be all you need to get started. I'll leave the actual code implementation to you.
Upvotes: 1