【发布时间】:2015-03-11 10:48:26
【问题描述】:
我正在制作一个简单的测验程序。我需要显示我的正确和错误,这取决于用户的回答。我认为它在 IF else 中。这就是为什么我无法通过它。当我运行它时。我选择正确的答案。它仍然显示“错误!”它认为它是正确的。然后更改为不同的数字。它仍然显示“错误!”。我使用复选框作为测验的多项选择。 这是我的代码:
if(C1.getState()) // if the user chooses the checkbox c1
{
outputlabel.setText("Correct\n");
CorrectAnswer++; // it will count one point per correct answer.
}else
outputlabel.setText("Wrong!\n");
if(C13.getState()) // if the user chooses the checkbox C13
{
outputlabel.setText("Correct\n");
CorrectAnswer++;
}else
outputlabel.setText("Wrong!\n");
if(C19.getState()) // if the user chooses the checkbox C19
{
outputlabel.setText("Correct\n");
CorrectAnswer++;
}else
outputlabel.setText("Wrong!\n");
if(C21.getState()) // if the user chooses the checkbox C21
{
outputlabel.setText("Correct\n");
CorrectAnswer++;
}else
outputlabel.setText("Wrong!\n");
if(C27.getState()) // if the user chooses the checkbox C27
{
outputlabel.setText("Correct\n");
CorrectAnswer++;
}else
outputlabel.setText("Wrong!\n");
CorrectLabel.setText("Correct Answers: "+CorrectAnswer);
score =(CorrectAnswer*100)/5; // average of the quiz
if (score>=75)
{
scorelabel.setText("Grade: "+score+ "% ");
}else{
scorelabel.setText("Grade: "+score+"%.");
repaint();}
}
}
【问题讨论】:
-
能否提供一个可执行代码sn-p,让我测试一下?
-
代码没问题。我的猜测是 getState 方法并没有按照您的想法执行。使用 getState 的结果打印一行。另外,分数应该计算为 CorrectAnswer*5,否则永远不会超过 75(您将获得最高分数 5)。