我有一个 JTable,我想根据难度级别显示问题,程序正常执行,但问题是它只显示一个难度级别(困难问题)我想按难度级别查看所有问题?我需要对 JTable 上的所有 IF 求和?或者我在 IF 上遇到问题?
public void actionPerformed(ActionEvent arg0) {
categorie=GestionCategorieDelegate.doFindCategorieById(PreparerTest.idCategorie);
System.out.println(categorie);
if(Facile.getText().length()!=0)
{
questions=GestionTestDelegate.doPrepareRandomTest(Integer.parseInt(Facile.getText()), categorie,"Facile");
}
else
{
}
if(Moyen.getText().length()!=0)
{
questions=GestionTestDelegate.doPrepareRandomTest(Integer.parseInt(Moyen.getText()), categorie, "Moyen");
}
else
{
}
if(Difficile.getText().length()!=0)
{
questions=GestionTestDelegate.doPrepareRandomTest(Integer.parseInt(Difficile.getText()), categorie, "Difficile");
}
System.out.println(questions);
initDataBindings();
}
最佳答案
您的问题是由于在每个 if block 上,您为 questions
变量分配了一个新引用。所以你最终会完成你所做的最后一个作业,即“困难”的作业。您没有指定 questions
是什么类型的对象,但我假设它是 List
或其他 Collection
类型。尝试使用 Collections.add()
或 Collections.addAll()
,而不是分配。
关于java - 如何按难度级别显示所有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36938705/