所以我拥有的是,我有20个问题和5个问题的答案,我想要实现的是获得唯一的答案,将有
" a , b , c, d, e "
为每个问题选择。
每次用户进入测验时,所有问题和答案都将被独立加载。
我的主要问题是我无法获得独特的答案,
例如,问题1
What is the day
我最终得到了同样的答案
a ) monday b) tuesday c) monday d)thursday e) saturday.....
我无法加载不同的单一答案..
怎么可能,我尝试拼接数组,但它只会让事情搞砸。
答案 0 :(得分:0)
你可以这样做......
var anwers:Array = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
var selectedAnswers:Array = new Array(5); // array for selected answers
var tmp:Number = Math.floor(Math.random*5);
var correct:String = answers[0]; // let "Monday" be the correct answer
selectedAnswers[tmp] = correct; // correct answer set to a random position
for(var i:Number=0; i<5; i++){
tmp=Math.floor(Math.random*5);
if(selectedAnswers[i].equals(correct)) continue; //skip for the correct answer
if(selectedAnswers.indexOf(answers[tmp])<0){ // set answer if not present already
selectedAnswers[i] = answers[tmp];
} else {
i--; // loop again if answer is set earlier
}
}
以上将是完全随机的,但可能需要一些时间来选择最后的元素..
以下方法会非常快,但不是完全随机的
var anwers:Array = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
var selectedAnswers:Array = new Array(5); // array for selected answers
var tmp:Number = Math.floor(Math.random*5);
var correctIndex:Number = 0; // let "Monday" be the correct answer
var correct:String = answers[correctIndex];
selectedAnswers[tmp] = correct; // correct answer set to a random position
var j:Number = correctIndex; // temporary index
var offset:Number = 2; // unique for this answer set
// so that the answers won't overlap for 5 choices
for(var i:Number=0; i<4; i++){ // not completely random (yet give random answers for specific cases)
tmp++;
if(tmp==5) tmp = 0;
j += offset;
if(j>answers.length) j -= answers.length;
selectedAnswers[tmp] = answers[j];
}
这会从随机索引开始从answers
到selectedAnswers
分配值;