Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 随机测验法_Java_Arrays_Random_Methods - Fatal编程技术网

Java 随机测验法

Java 随机测验法,java,arrays,random,methods,Java,Arrays,Random,Methods,每个方法都包含一个具有多项选择的问题。当我在main中调用该方法时,我需要将其洗牌,并确保没有重复 public static void main(String[] args) { question_1(); question_2(); question_3(); question_4(); //continue to question 15 question_15(); } 我试过的东西 int question_A = questi

每个方法都包含一个具有多项选择的问题。当我在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];
}

对于random,我也尝试了任何方法,比如
Math.random
。这些都不起作用,是的,请不要用先进的技术来解决这个问题,因为我是初学者。

看,我不知道你在用这些方法做什么,但是让我们考虑把其他问题放到数据库中的另一种方法,在你的java代码中使用类似say hashmap的集合访问你的数据库,根据问题的id,你可以调用任何你想调用的问题,对于洗牌部分,java中有一个预定义的函数称为shuffle,你可以使用它来洗牌你的问题集合。只是一个建议,试试看,我认为这是一个更好的方法。

简单的方法是使用列表:

List<Question> questions = new ArrayList<Question>();
questions.add(question_1);
questions.add(question_2);
questions.add(question_3);
.....
Collections.shuffle(questions);
List questions=new ArrayList();
添加问题(问题1);
添加问题(问题2);
添加问题(问题3);
.....
收集。洗牌(问题);

您可以这样做

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;
}
publicstaticvoidmain(字符串[]args){
//首先声明变量
int q1=1;
int q2=2;
...
int q15=15;
int[]问题=新的int[]{q1,q2,q3,q4,…q15};
System.out.println(“洗牌前”);
用于(int i:问题){
系统输出打印LN(i);
}
洗牌(问题);//我们来洗牌
System.out.println(“洗牌后”);
用于(int i:问题){
系统输出打印LN(i);
}
}
公共静态无效洗牌(int[]问题){
随机=新随机();
for(int i=0;i
也许您可以创建数组,然后洗牌数组,然后迭代洗牌数组。这样似乎能满足您的需要(而且简单得多)。先生,您能给我一个example@Yogesh,所以有很多这样的例子,如果你被卡住了,大声喊[问一个问题]。。。声明一个数组->。洗牌数组->最后迭代数组->
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;
}