繁体   English   中英

如何通过覆盖哈希码和equals方法生成唯一问题

[英]how to generate unique questions by overriding hashcode and equals method

我的程序从QuestionBank.sql文件读取问题详细信息。 一切都很好,但是输出包含10个问题,而不是得到12个问题。

输出为:

GK简单
GK简单
GK中
GK综合体
科学综合体
历史媒介
历史媒介
历史简单
历史简单
地理媒介

**DataManagerImpl.java**

@Override
    public Set<Question> generateQuestionPaper(List<Question> list,
            List<Criteria> template) {
        // TODO Auto-generated method stub
        Set<Question> questionSet = new HashSet<Question>();
        int count;
        int index = 0;
        for(Criteria c: template){
            count = 0;
            while(c.getNoOfQuestion() > count){
                index = (int)(Math.random()*list.size());
                //System.out.println(index);
                Question q = list.get(index);
                if(c.getCategory().equals(q.getCategory()) && c.getComplexity().equals(q.getComplexity()) ){
                    if(!questionSet.contains(q)){
                        count++;
                        questionSet.add(q);
                        System.out.println(q.getCategory()+" "+q.getComplexity());
                    }

                }                   
            }
        }
        return questionSet;
    }

Criteria.java

public class Criteria {

private Category category;
private Complexity complexity;
private int noOfQuestion;

public Criteria() {
}

public Criteria(Category category, Complexity complexity,int noOfQuestion) {
    super();
    this.noOfQuestion = noOfQuestion;
    this.category = category;
    this.complexity = complexity;
}

public int getNoOfQuestion() {
    return noOfQuestion;
}

public void setNoOfQuestion(int noOfQuestion) {
    this.noOfQuestion = noOfQuestion;
}

public Category getCategory() {
    return category;
}

public void setCategory(Category category) {
    this.category = category;
}

public Complexity getComplexity() {
    return complexity;
}

public void setComplexity(Complexity complexity) {
    this.complexity = complexity;
}

}

列表模板包含:(作为参数传递给generateQuestionpaper() 在此处输入图片说明

请帮我!!

问题在于Math.random()方法的定义。

如下修改您的代码后尝试-

Random random = new Random();
for(Criteria c: template){
        count = 0;
        while(c.getNoOfQuestion() > count){
            index = random.nextInt(list.size());

由于列表索引也是基于零的,因此应该可以正常工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM