-2

Each method contain a question with multiple choice. When i call the method in the main, i need to shuffle it and make sure there are no repetition.

public static void main(String[] args) { 
    question_1();
    question_2();
    question_3();
    question_4();
    //continue to question 15
    question_15();
    }

thing that i tried.

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];
}

The reason the random does not work is because it starts executing the program at

int question_A = question_1();

for the random, i also tried any way such as Math.random. None of these worked and yeah, please do not use advance technique to solve this problem as i am a beginner.

Rajesh N
  • 2,426
  • 1
  • 11
  • 16
Yogesh M
  • 13
  • 1
  • 8
  • 3
    Perhaps you might create the array, then shuffle the array, then iterate the shuffled array. Such would seem to meet your needs (and be a lot simpler). – Elliott Frisch May 22 '15 at 04:17
  • Sir, could you give me an example – Yogesh M May 22 '15 at 04:19
  • 2
    @Yogesh, SO has many examples of this, shout [ask a question] if you get stuck ... Declare an array -> http://stackoverflow.com/questions/1200621/declare-array-in-java . Shuffle an array -> http://stackoverflow.com/questions/1519736/random-shuffling-of-an-array and finally iterate an array -> http://stackoverflow.com/questions/3010840/loop-through-array-in-javascript – Kenneth Clark May 22 '15 at 04:22

3 Answers3

1

The easy way to do this is using a list:

List<Question> questions = new ArrayList<Question>();
questions.add(question_1);
questions.add(question_2);
questions.add(question_3);
.....
Collections.shuffle(questions);
Eduardo Pérez
  • 458
  • 3
  • 10
0

See, I dunno what you are doing with all these methods, but let us consider another approach put every question in a database, use a Collection like say hashmap in ur java code access ur database and based on the id of the question u can call whichever question u want to call and for the shuffling part there is a predefined function called shuffle in java, u can use it to shuffle ur question collection. Just a suggestion, try it, i think it is a better approach.

sapamlucy
  • 298
  • 1
  • 2
  • 15
0

You could do something like this

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;
}
khakiout
  • 2,285
  • 21
  • 30