[英]How do I make an answer able to have more than one word in my java quiz?
[英]How do I make it so my quiz lets me answer?
我是一个初学者,我正在尝试编写一个简单的程序,先问一个问题,然后提示您在输入对话框中输入“ A”,“ B”或“ C”的形式回答该问题,但没有对话框出现。 其他一切似乎都正常。
这是我的代码:
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.");
}
}
提前致谢。
为清楚起见进行了编辑。
char playerAnswer = playerAnswerString.charAt(0);
用户在此处选择答案后,必须分配playerAnswer的值
playerAnswerString = JOptionPane.showInputDialog(null, "A = Blue, B = Green, C = Brown");
否则变量playerAnswer将为空
所做的修改很少,请检查并让我知道代码是否按您的期望工作。
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.");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.