Skip to content

Instantly share code, notes, and snippets.

@AnEmortalKid
Created March 20, 2016 21:11
Show Gist options
  • Save AnEmortalKid/317dd4ae8613e946bafc to your computer and use it in GitHub Desktop.
Save AnEmortalKid/317dd4ae8613e946bafc to your computer and use it in GitHub Desktop.
The final sample of the Quiz displayer
public class QuizDisplayer {
private static boolean displayQuestion(Question question) {
int optionChosen = JOptionPane.showOptionDialog(null, question.getQuestion(), "Please Select",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, question.getAnswerOptions(),
question.getAnswerOptions()[0]);
String answer = question.getAnswerOptions()[optionChosen];
boolean correct = answer.equals(question.getCorrectAnswer());
return correct;
}
public static void main(String[] args) {
// Creating our 3 questions
String[] options = { "True", "False" };
Question first = new Question("The first part of any method is its header.", "True", options);
String[] q2Options = {"True", "False"};
Question second = new Question("A line of code that declares a variable is known as a varibale declaration", "True", q2Options);
//Yeah, we can do stuff that is not just True or False!
String[] q3Options = {"A", "B", "C"};
Question thirdQuestion = new Question("What is the second letter in the alphabet", "B", q3Options);
// Store them in our quiz questions array
Question[] quizQuestions = new Question[3];
quizQuestions[0] = first;
quizQuestions[1] = second;
quizQuestions[2] = thirdQuestion;
//Now a score
int score = 0;
//and the enhanced for loop
for (Question question : quizQuestions) {
boolean isCorrect = displayQuestion(question);
if(isCorrect)
{
score++;
}
}
//At the end we display the score
System.out.println("You got " + score + "/3 questions correct");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment