简体   繁体   中英

Java- Assigning random number to a do while loop

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

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 if s.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM