首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过重写哈希代码和等于方法生成唯一的问题

如何通过重写哈希代码和等于方法生成唯一的问题
EN

Stack Overflow用户
提问于 2016-05-31 10:00:28
回答 1查看 36关注 0票数 0

我的程序从QuestionBank.sql文件中读取问题细节。所有的问题都是正确的,但是没有得到12个问题,而是输出了10个问题。

输出是:

GK简单 GK简单 GK介质 GK复合体 科学情结 历史媒介 历史媒介 历史简约 历史简约 地理媒介

代码语言:javascript
复制
**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

代码语言:javascript
复制
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() )

请救救我!

EN

回答 1

Stack Overflow用户

发布于 2016-05-31 12:36:17

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

修改代码后,尝试如下所示-

代码语言:javascript
复制
Random random = new Random();
for(Criteria c: template){
        count = 0;
        while(c.getNoOfQuestion() > count){
            index = random.nextInt(list.size());

因为,列表索引也是基于零的,这应该很好。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37542573

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档