Here is one last activity: Write an application that contains an array of 10 mul
ID: 3673834 • Letter: H
Question
Here is one last activity:
Write an application that contains an array of 10 multiple-choice quiz questions related to your favorite hobby. Each question contains three answer choices. Also create an array that holds the correct answer to each question -- A, B or C. Display each question and verify that the user enters only A,B, or C as the answer -- if not, keep prompting the user until a valid response is entered. If the user responds to a question correctly, display "Correct!"; otherwise, display "The correct answer is" and the letter of the correct answer. After the user answers all the questions, display the number of correct and incorrect answers. Save the file as Quiz.java.
Explanation / Answer
import javax.swing.JOptionPane; public class Quiz { /** * isValid accapts a string and checks to see if it is A, B, or C * and returns a boolean value of true or false if it matches */ private static boolean isValid(String a) { a = a.toLowerCase(); if(a.equals("a") || a.equals("b") || a.equals("c")) { return true; } else { JOptionPane.showMessageDialog(null, "Please pick A, B, or C"); return false; } } /** * askQuestion recieves an array containing the question and all the * answers. Returning the user input as a String */ private static String askQuestion(String[] q) { String fmtQuestion = ""; // String to hold the formatted question String answer = ""; for(int i = 0; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.