关于随机生成指定分值的试卷问题

ZZZ5512536 2011-11-08 03:09:49
题库里有三种题型,选择,判断,填空。而每道题都有各自的分值,即同样是选择题也有1,2,3分的差别。

现在要指定每种题型的题目数量来组成试卷,还指定试卷的总分,比如100分。

假定题库不会出现题目不够的情况,求好点的思路,能贴出相关代码最好~

谢谢
...全文
249 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangli96333 2011-11-08
  • 打赏
  • 举报
回复
思路,获取总题目数,然后先每个题目设置1分,然后看100-总分后还剩多少分
循环剩余分数,随机取1道题加1分,如果加到最大3分,则排除出下一次随机,直到剩余分数用完
最后再把所有的题目随机打乱一次顺序

for example

Java code

public static void randomTest(int[] tests) { if (tests == null || tests.length == 0) { System.out.println("error"); return; } int sum = 0; for (int i : tests) { sum += i; } if (sum > 100) { System.out.println("error"); return; } List<Integer> questions = new LinkedList<Integer>(); //题目 List<Integer> maxScore = new ArrayList<Integer>(); //最大分数 for (int i=0; i<sum; i++) { questions.add(1); } int remain = 100 - sum; while (remain > 0) { //剩余分数还有 int s = questions.remove((int)Math.random()*(questions.size())); s++; //随机取一道题加1分 if (s == 3) { //如果分数最大,则排除下一次随机 maxScore.add(s); } else { //否则继续参与下一次随机 questions.add(s); } remain--; } questions.addAll(maxScore); //获得所有题目 Collections.shuffle(questions); //随机排列所有题目 for (int i=0; i<sum; i++) { //打印题目 if (i < tests[0]) { System.out.printf("select question, score[%d]\n", questions.get(i)); } else if (tests.length > 1 && i < tests[0] + tests[1]) { System.out.printf("judgment question, score[%d]\n", questions.get(i)); } else { System.out.printf("filling blank question, score[%d]\n", questions.get(i)); } } }

qybao 2011-11-08
  • 打赏
  • 举报
回复
怎么没体现
调用的时候
randomTest(new int[]{10, 20, 30}); //分别对应10道选择题,20道判断题,30道填空题
具体什么类型的题目有多少题,通过参数数组设定
ZZZ5512536 2011-11-08
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 qybao 的回复:]

思路,获取总题目数,然后先每个题目设置1分,然后看100-总分后还剩多少分
循环剩余分数,随机取1道题加1分,如果加到最大3分,则排除出下一次随机,直到剩余分数用完
最后再把所有的题目随机打乱一次顺序

for example
Java code
public static void randomTest(int[] tests) {
if (tests == null ||……
[/Quote]

看了一下你的思路,貌似你这个不能体现每个题型的指定数目?
24K純帥 2011-11-08
  • 打赏
  • 举报
回复
每种类型的题目都是固定个数,每种类型在题库里面随机抽取
qybao 2011-11-08
  • 打赏
  • 举报
回复
忘了一个判断
if (sum*3 < 100) { //如果最大分数都不能达到100,也是不合理的
System.out.println("error");
return;
}

qybao 2011-11-08
  • 打赏
  • 举报
回复
思路,获取总题目数,然后先每个题目设置1分,然后看100-总分后还剩多少分
循环剩余分数,随机取1道题加1分,如果加到最大3分,则排除出下一次随机,直到剩余分数用完
最后再把所有的题目随机打乱一次顺序

for example
public static void randomTest(int[] tests) {
if (tests == null || tests.length == 0) {
System.out.println("error");
return;
}

int sum = 0;
for (int i : tests) {
sum += i;
}

if (sum > 100) {
System.out.println("error");
return;
}

List<Integer> questions = new LinkedList<Integer>(); //题目
List<Integer> maxScore = new ArrayList<Integer>(); //最大分数
for (int i=0; i<sum; i++) {
questions.add(1);
}

int remain = 100 - sum;
while (remain > 0) { //剩余分数还有
int s = questions.remove((int)Math.random()*(questions.size()));
s++; //随机取一道题加1分
if (s == 3) { //如果分数最大,则排除下一次随机
maxScore.add(s);
} else { //否则继续参与下一次随机
questions.add(s);
}
remain--;
}

questions.addAll(maxScore); //获得所有题目
Collections.shuffle(questions); //随机排列所有题目

for (int i=0; i<sum; i++) { //打印题目
if (i < tests[0]) {
System.out.printf("select question, score[%d]\n", questions.get(i));
} else if (tests.length > 1 && i < tests[0] + tests[1]) {
System.out.printf("judgment question, score[%d]\n", questions.get(i));
} else {
System.out.printf("filling blank question, score[%d]\n", questions.get(i));
}
}
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧