每种方法都包含多项选择的问题。当我在main中调用方法时,我需要将其洗牌并确保没有重复。
public static void main(String[] args) {
question_1();
question_2();
question_3();
question_4();
//continue to question 15
question_15();
}
我试过的东西。
int question_A = question_1();
int question_B = question_2();
int question_C = question_3();
int question_D = question_4();
//to question 15
int question_O = question_15();
//then i created an array
int [] question = new int[14];
question[0] = question_A;
question[1] = question_B;
question[2] = question_C;
question[3] = question_D;
//to last array
question[14] = question_O;
//to random it here is the code
Random r = new Random();
for (int counter = 0; counter <10; ++counter){
int swap_Index = r.next Int(15-counter)+counter; //there is an space between next Int, that because i was getting not properly formatted in the edit box
int temp = question[counter];
question[counter] = question[swap_Index];
question[swap__Index] = temp;
int[] question_To_Ask = new int[10];
for (int count = 0; count<10; ++count){
question_To_Ask[count] = question[count];
}
随机无效的原因是因为它开始执行
程序int question_A = question_1();
对于随机,我也试过任何方式,如Math.random
。这些都不起作用,是的,请不要使用先进的技术来解决这个问题,因为我是初学者。
答案 0 :(得分:1)
执行此操作的简单方法是使用列表:
List<Question> questions = new ArrayList<Question>();
questions.add(question_1);
questions.add(question_2);
questions.add(question_3);
.....
Collections.shuffle(questions);
答案 1 :(得分:0)
请注意,我不知道你在使用所有这些方法做了什么,但是让我们考虑另一种方法将每个问题都放在数据库中,在ur java代码访问ur数据库中使用类似于hashmap的集合并基于问题的id你可以调用你想要调用的任何问题,并且对于shuffling部分,在java中有一个名为shuffle的预定义函数,你可以使用它来改变你的问题集合。只是一个建议,尝试一下,我认为这是一种更好的方法。
答案 2 :(得分:0)
你可以做这样的事情
public static void main(String[] args) {
// declare the variables first
int q1 = 1;
int q2 = 2;
...
int q15 = 15;
int[] questions = new int[] { q1, q2, q3, q4, ... q15 };
System.out.println("Before Shuffle");
for (int i : questions) {
System.out.println(i);
}
shuffle(questions); // here we do the shuffle
System.out.println("After Shuffle");
for (int i : questions) {
System.out.println(i);
}
}
public static void shuffle(int[] questions) {
Random random = new Random();
for (int i = 0; i < questions.length; i++) {
int newIndex = random.nextInt(questions.length - 1);
swap(questions, i, newIndex);
}
}
private static void swap(int[] questions, int oldIndex, int newIndex) {
int temp = questions[oldIndex];
questions[oldIndex] = questions[newIndex];
questions[newIndex] = temp;
}