programming
import javax.swing.JOptionPane; public class Quiz { //static int variables that counts number of question and correct answers. static int nQuestions = 0; static int nCorrect = 0; public static void main(String[] args) { String question1 = "What is java?\n"; question1 += "A. A shorthand for javascript.\n"; question1 += "B. A programing language.\n"; question1 += "C. A machine readable code.\n"; question1 += "D. An organization for creating code syntax.\n"; question1 += "E. None of the above."; check(question1, "C"); String question2 = "What is the popular search engine\n"; question2+="A. Yahoo\n"; question2+="B. Google\n"; question2+="C. Bing\n"; question2+="D. Duck Duck Go\n"; check(question2,"B"); //question 3. String question3; question3 = "What is the color of the sky at night \n"; question3+="A. Red\n"; question3+="B. Blue\n"; question3+="C. Black \n"; question3+="D. Red\n"; check(question3,"C"); //prints number of correct answers out of number of questions. JOptionPane.showMessageDialog(null, nCorrect + " correct out of " + nQuestions + " questions"); } //method ask. public static String ask(String question) { //keep asking if the answer entered is Invalid 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"))){ JOptionPane.showMessageDialog(null,"Invalid Answer. Please enter A, B, C, D, or E."); continue; } return answer; //return answer entered by user. } } //method check static void check(String question, String correctAnswer) { nQuestions++;//increments the number of question. String answer = ask(question);//calls ask method to ask question. if(answer.equals(correctAnswer)) { JOptionPane.showMessageDialog(null,"Correct!"); nCorrect++;//increments if answer is correct. } else{ JOptionPane.showMessageDialog(null, "Incorrect. The correct answer is " + correctAnswer); } } }