【问题标题】:Displaying "Correct" and "Wrong" in JAVA在 JAVA 中显示“正确”和“错误”
【发布时间】: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)。

标签: java checkbox settext


【解决方案1】:

我不完全确定您要在代码中做什么。对于每个您正在检查它是否已设置,然后您正在设置 outputlabel 值。因此,如果选中 first 复选框,它会将输出标签文本设置为“正确”。如果 任何 其他复选框被 选中,那么它将简单地覆盖您之前所做的并将标签设置为“错误”。

也许您希望每个复选框都有单独的输出标签?

【讨论】:

  • 另外,if(C??.getState()) // if the user chooses the checkbox C?? { outputlabel.setText("Correct\n"); CorrectAnswer++; }else outputlabel.setText("Wrong!\n"); 好像是copy-pasta。我建议把它放在一个单独的方法中。您的复选框可以放入容器中,例如数组或List
【解决方案2】:

在检查所有正确答案的状态后,您应该有一个最终输出标签。并根据正确答案计数,您可以设置最终输出标签。

【讨论】:

    猜你喜欢
    • 2015-07-03
    • 2013-09-14
    • 2014-12-06
    • 1970-01-01
    • 2021-12-22
    • 1970-01-01
    • 2017-09-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多