I am a beginning programmer, and I am trying to write a simple program that asks a question then prompts you for an answer to that question in the form of entering "A", "B", or "C" in the input dialog, but no dialog box is appearing. Everything else seems to work fine.
Here is my code:
package homework;
import javax.swing.JOptionPane;
public class Quiz {
public static void main(String[] args)
{
int x = 0;
String[] quizQuestion = {"What is the color of the sky?", "What is the
color of the sea?", "What is the color of the earth?"};
int score = 0;
String correct = "You are correct";
String incorrect = "You are incorrect";
String playerAnswerString = " ";
playerAnswerString.toUpperCase();
char playerAnswer = playerAnswerString.charAt(0);
JOptionPane.showMessageDialog(null, "Test Your Knowledge!");
JOptionPane.showMessageDialog(null, "Select an answer to the questions.");
for(x = 0; x < 3; x++)
{
JOptionPane.showMessageDialog(null, quizQuestion[x]);
while(quizQuestion.equals(0))
{
playerAnswerString = JOptionPane.showInputDialog(null, "A = Blue, B = Green, C = Brown");
if(playerAnswer == 'A')
{
JOptionPane.showMessageDialog(null, correct);
score++;
}
else
{
JOptionPane.showMessageDialog(null, incorrect);
}
}
while(quizQuestion.equals(1))
{
playerAnswerString = JOptionPane.showInputDialog(null, "A = Blue, B = Green, C = Brown");
if(playerAnswer == 'B')
{
JOptionPane.showMessageDialog(null, correct);
score++;
}
else
{
JOptionPane.showMessageDialog(null, incorrect);
}
}
while(quizQuestion.equals(2))
{
playerAnswerString = JOptionPane.showInputDialog(null, "A = Blue, B = Green, C = Brown");
if(playerAnswer == 'C')
{
JOptionPane.showMessageDialog(null, correct);
score++;
}
else
{
JOptionPane.showMessageDialog(null, incorrect);
}
}
}
JOptionPane.showMessageDialog(null, "You scored " + score + "/3.");
}
}
Thanks in advance.
Edited for clarity.
Few modifications done, please check and let me know if code works as per your expectations.
import javax.swing.JOptionPane;
public class Quiz {
public static void main(String[] args)
{
int x = 0;
String[] quizQuestion = {"What is the color of the sky?", "What is the color of the sea?", "What is the color of the earth?"};
int score = 0;
String correct = "You are correct";
String incorrect = "You are incorrect";
String playerAnswerString = " ";
playerAnswerString.toUpperCase();
char playerAnswer = playerAnswerString.charAt(0);
JOptionPane.showMessageDialog(null, "Test Your Knowledge!");
JOptionPane.showMessageDialog(null, "Select an answer to the questions.");
for(x = 0; x < 3; x++)
{
JOptionPane.showMessageDialog(null, quizQuestion[x]);
if(x==0)
{
playerAnswerString = JOptionPane.showInputDialog(null, "A = Blue, B = Green, C = Brown");
System.out.println(playerAnswerString+" "+playerAnswer);
if(playerAnswerString.equals("A"))
{
JOptionPane.showMessageDialog(null, correct);
score++;
}
else
{
JOptionPane.showMessageDialog(null, incorrect);
}
}
if(x==1)
{
playerAnswerString = JOptionPane.showInputDialog(null, "A = Blue, B = Green, C = Brown");
if(playerAnswerString.equals("B"))
{
JOptionPane.showMessageDialog(null, correct);
score++;
}
else
{
JOptionPane.showMessageDialog(null, incorrect);
}
}
if(x==2)
{
playerAnswerString = JOptionPane.showInputDialog(null, "A = Blue, B = Green, C = Brown");
if(playerAnswerString.equals("C"))
{
JOptionPane.showMessageDialog(null, correct);
score++;
}
else
{
JOptionPane.showMessageDialog(null, incorrect);
}
}
}
JOptionPane.showMessageDialog(null, "You scored " + score + "/3.");
}
}