【发布时间】:2015-05-22 04:16:09
【问题描述】:
每个方法都包含一个带有多项选择的问题。当我在 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。这些都不起作用,是的,请不要使用高级技术来解决这个问题,因为我是初学者。
【问题讨论】:
-
也许您可以创建数组,然后打乱数组,然后迭代打乱的数组。这样似乎可以满足您的需求(并且要简单得多)。
-
先生,你能举个例子吗
-
@Yogesh,所以有很多这样的例子,如果你卡住了,请喊 [ask a question] ...声明一个数组 -> stackoverflow.com/questions/1200621/declare-array-in-java 。打乱一个数组 -> stackoverflow.com/questions/1519736/… 最后迭代一个数组 -> stackoverflow.com/questions/3010840/…
标签: java arrays random methods