Question
I am currently busy with my programming assignment. However, I got stuck on this section below. Im not sure how to do this part. I
I am currently busy with my programming assignment. However, I got stuck on this section below. Im not sure how to do this part. I have also provided the code I have done so far.:
- Modify your program so that it responds differently to different input.
- It should display one message if the answer is correct, like "Correct!"
- It should display a different message if the answer is valid ("A", "B", "C", "D", or "E") but
- incorrect, like "Incorrect. Please try again."
- It should display a third message if the answer is invalid (anything other than single characters
- "A", "B", "C", "D", and "E"), like "Invalid answer. Please enter A, B, C, D, or E." Run your program and test each of these possibilities.
- Modify your program so that it asks the question and responds with a dialog until the user enters the correct answer.
- Move the statements that record the answer and display a response into a while loop.
- Use "break" or "return" to exit the loop when the answer is correct.
HERE is the code I have done so far:
import javax.swing.JOptionPane;
public class Quiz {
public static void main(String[] args) {
// TODO Auto-generated method stub
String question = "which of the following is an example of mexican food? ";
question += "A. Hamburger ";
question += "B. Pizza ";
question += "C. Fries ";
question += "D. Pasta ";
question += "E. Tacos ";
String answer = JOptionPane.showInputDialog(question);
answer = answer.toUpperCase();
if (answer.equals("E")) {
JOptionPane.showMessageDialog(null,"Correct!");
}
if (answer.equals("A")) {
JOptionPane.showMessageDialog(null,"Try again!");
}
if (answer.equals("B")) {
JOptionPane.showMessageDialog(null,"Better luck next time!");
}
if (answer.equals("C")) {
JOptionPane.showMessageDialog(null,"Try again!");
}
if (answer.equals("D")) {
JOptionPane.showMessageDialog(null,"Try again!");
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started