Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

import javax.swing.

JOptionPane;

public class Quiz{

static int nQuestions = 0;

static int nCorrect = 0;

static String ask (String question) {

while(true) {

String answer = JOptionPane.showInputDialog(question);

answer = answer.toUpperCase();

if ((answer.equals("A") || answer.equals("B") || answer.equals("C") || answer.equals("D") || answer.equals("E"))){

return answer.toUpperCase();

}else {JOptionPane.showMessageDialog(null," Invalid answer. Please enter A, B, C, D, or E.");}

static void check (String question, String correctAnswer) {

String answer = ask (question);

if (answer.equals(correctAnswer)){

JOptionPane.showMessageDialog(null," Correct! ");

nCorrect++;

}else {JOptionPane.showMessageDialog(null," Incorrect " + "- The correct answer is: " + correctAnswer);}

nQuestions++;

public static void main(String[] args) {

String question = "Which assignment is this?\n";

question += "A. The discution Assignment\n";

question += "B. The Learning Journal\n";

question += "C. The Programming Assignment\n";

question += "D. The Reading Assignment\n";

question += "E. The Self Quiz\n";

check(question,"A");

question = "how old am I?\n";


question += "A. 25 years old\n";

question += "B. 19 years old\n";

question += "C. 42 years old\n";

question += "D. 27 years old\n";

question += "E. 100 years old";

check(question,"B");

question = "On which planet do we live?\n";

question += "A. Earth\n";

question += "B. Mars\n";

question += "C. Venus\n";

question += "D. Saturn\n";

question += "E. Neptune";

check(question,"A");

JOptionPane.showMessageDialog(null,nCorrect+" correct out of "+nQuestions+" questions");

You might also like