Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (1 vote)
1K views

CS 1102 Unit 2 Programming Assignment Solution

This document provides an example Java code for a quiz program that asks the user a multiple choice question about the definition of a quiz. The code displays the question and five possible answers, checks the user's response, and notifies them if their answer is correct or incorrect before terminating the loop.

Uploaded by

mona kweder
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
1K views

CS 1102 Unit 2 Programming Assignment Solution

This document provides an example Java code for a quiz program that asks the user a multiple choice question about the definition of a quiz. The code displays the question and five possible answers, checks the user's response, and notifies them if their answer is correct or incorrect before terminating the loop.

Uploaded by

mona kweder
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

CS 1102 - Unit 2 Solution

The quiz question, answers, and choice for correct answer should be unique for each submission. Here is
an example.

import javax.swing.JOptionPane;

public class Quiz {

public static void main(String[] args) {

String question = "What is a quiz?\n";

question += "A. a test of knowledge, especially a brief


informal test given to students\n";

question += "B. 42\n";

question += "C. a duck\n";

question += "D. to get to the other side\n";

question += "E. To be or not to be, that is the question.";

while (true) {

String answer = JOptionPane.showInputDialog(question);

answer = answer.toUpperCase();

if (answer.equals("A")) {

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

break;

} else if (answer.equals("B") || answer.equals("C") ||


answer.equals("D") || answer.equals("E")) {

JOptionPane.showMessageDialog(null,"Incorrect. Please
try again.");

} else {

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

}
}

You might also like