0

What I'm attempting to do is have 2 or three do while loops that each have 10 or so if statements within that contain questions. Each if statement (question) is assigned a number (generated by random num gen) and triggers a different question. I want them to trigger randomly when the program is run- So if you run it once the 3rd question in the list might trigger first and the next time the 7th question might trigger first. A sample do while loop is below:

 do {

         i++;
         //set what variable you want to represent random vars
         randomint = randomGenerator.nextInt(10); 
         System.out.println(randomint);

         /*
          * need to generate 1 number per do loop (as opposed to 10 each loop which this is doing)
          * and make sure that the numbers 1-10 are all hit within the 10 cycles of the do loop
          * then will do again for a harder set of questions in the second loop
          */

         if(randomint==1) { 
             System.out.println("What is the capital of PA?"); 
             guess= in.nextLine();
             if(answer1a.equals(guess) || answer1b.equals(guess)) {
                 System.out.println("Correct! Good job!");
                 score=score+5;
             }
             /*
              * add another for loop that gives 4,3,2,1,0 points based on # of guesses used
              */

             else {
                 do {
                 System.out.println("Nope, try again!");
                 guess= in.nextLine();
                 if (answer1a.equals(guess) || answer1b.equals(guess))
                     System.out.println("Correct! Good Job!");

                 }while (!answer1a.equals(guess) && !answer1b.equals(guess));
              }

         }

     } while (i !=10);

So that same "if" statement will be repeated for ==2,==3, etc.. for different questions Obviously the problem here is that every time the do loop repeats I generate a completely new set of 10 random numbers. Is there a way to generate 10 random numbers but it stops after each one and scans through my if statements so it picks one, then continues onto the second value in the random number generator? I want this to ask each individual question (10) and then exit the original do loop as determined by my i++ count.

I did try to search for this but was having trouble finding anything- It might possible be a term tat I havent come across yet. Thanks all

user3803709
  • 107
  • 2
  • 9
  • "generate 10 random numbers but it stops after each one and scans through my if statements so it picks one", what your current code does ? I think it does like what you want.. – Bla... Jul 06 '14 at 13:59
  • The problem is I am generating 10 different numbers each time. So the first time around the number "1" might generate in the first spot 2 times, causing the first question to trigger twice. By the time my counter (i++) gets to 10, I wont have hit all numbers 1-10 because some of the questions might have been generated multiple times – user3803709 Jul 06 '14 at 14:11
  • Read the [javadocs](http://docs.oracle.com/javase/8/docs/api/java/util/Random.html)! You seem to operating be under the misconception that `randomGenerator.nextInt(10)` generates 10 numbers. It doesn't, it generates one value in the range [0,9]. The generated values are supposed to behave like independent random numbers, would you be surprised if you rolled a 6-sided die 6 times and got a few duplicate values? For your case, the probability of getting 10 distinct values is 0.00036288, very unlikely. – pjs Jul 06 '14 at 14:53

2 Answers2

0

Prior to your do-while loop, create an ArrayList with the ten numbers. Shuffle the ArrayList. Then change your do-while to an iterator loop over the shuffled values.

I'd also recommend using a switch statement rather than a series of ifs.

pjs
  • 18,696
  • 4
  • 27
  • 56
0

Use an Array to save all generated value, for the next iteration..

int[] anArray; //use this array to save all showed question
anArray = new int[10];     

int i = 0;
//set what variable you want to represent random vars
randomint = randomGenerator.nextInt(10); 

do{

   if(i > 0){ 
       //if there is min. 1 value in the array check if the next 
       //random value was already in the array
       while(Arrays.asList(anArray).contains(randomint) == true){
           randomint = randomGenerator.nextInt(10); 
       }
   }
   anArray[i] = randomint; //save the new value for the next checking
   i++;
   System.out.println(randomint);

   //the Question if statements goes here...
}while (i !=10);

OR, you can use Shuffle to shuffle the array ordering, see code below:

  public static void main(String args[])
  {
    int[] solutionArray = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

    shuffleArray(solutionArray);
    for (int i = 0; i < solutionArray.length; i++)
    {
      System.out.print(solutionArray[i] + " ");
    }
    System.out.println();
  }

  // Implementing Fisher–Yates shuffle
  static void shuffleArray(int[] ar)
  {
    Random rnd = new Random();
    for (int i = ar.length - 1; i > 0; i--)
    {
      int index = rnd.nextInt(i + 1);
      // Simple swap
      int a = ar[index];
      ar[index] = ar[i];
      ar[i] = a;
    }
  }
Community
  • 1
  • 1
Bla...
  • 7,228
  • 7
  • 27
  • 46
  • I'm having a very tough time getting either of these to work. I'm going to look more into Arrays and see if I can figure out why this isn't working for me. Thanks for the response- I'll look more into it when I understand exactly what these statements mean instead of simply inputting them into my code. – user3803709 Jul 07 '14 at 02:02