我正在制作一个简单的测验程序。我需要显示我的正确与错误,这取决于用户的回答。我认为是在其他情况下。这就是为什么我无法通过它。当我运行它时。我选择正确的答案。它仍然显示“错误!”并认为它是正确的。然后更改为其他数字。它仍然显示“错误!”。我使用复选框作为测验的多项选择。
这是我的代码:

    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();}

    }

    }

最佳答案

我不确定您要在代码中做什么。对于每个,您都要检查它是否已设置,然后再设置outputlabel值。因此,如果选中第一个复选框,它将输出标签文本设置为“正确”。并且,如果未选中其他任何复选框,则它将仅覆盖您之前所做的操作,并将标签设置为“错误”。

也许您想为每个复选框使用单独的outputlabel?

10-07 19:20
查看更多