Skip to content
Sign upLog in
← Back to Community

Java programming

Profile icon
mwabonje1

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.");
}
}
}

Need to amend the above as per the attached instruction

Invalid image url

Voters
Profile icon
mwabonje1
Comments
hotnewtop
Profile icon
Highwayman

Could you clarify? Also, it is preferred that you give us

  1. either the repl with this code or the code in a code block
  2. the specific error that is giving you trouble
  3. no homework questions or "just do it" commands
    Thank you! :)
Profile icon
mwabonje1

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.");
}
}
}

I wanted the above code to ask multiple question and return the number of correct responses

Profile icon
Highwayman

@mwabonje1
what is the error.

Profile icon
Highwayman

OP’s code formatted:

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."); } } }