Java-为do while循环分配随机数

时间:2014-07-06 13:46:53

标签: java random

我尝试做的是有两个或三个while循环,每个循环有10个左右if语句包含问题。每个if语句(问题)都被赋予一个数字(由随机数生成)并触发一个不同的问题。我希望它们在程序运行时随机触发 - 所以如果你运行它,一旦列表中的第三个问题可能先触发,下一次第七个问题可能先触发。下面的循环示例如下:

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

如此"如果"对于不同的问题,将重复== 2,== 3等语句 显然这里的问题是每次do循环重复时我都会生成一组全新的10个随机数。有没有办法生成10个随机数,但它会在每个数字后停止并扫描我的if语句,以便选择一个,然后继续到随机数生成器中的第二个值?我希望这可以询问每个问题(10)然后退出原始的do循环,这是由我的i ++计数确定的。

我确实试图寻找这个但却找不到任何东西 - 这可能是一个我还没有遇到的术语。谢谢大家

2 个答案:

答案 0 :(得分:0)

do-while循环之前,创建一个包含十个数字的ArrayList。 Shuffle ArrayList。然后将改变后的值do-while更改为迭代器循环。

我还建议使用switch语句而不是一系列if

答案 1 :(得分:0)

使用数组保存所有生成的值,用于下一次迭代..

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

或者,您可以使用Shuffle来重新排列数组顺序,请参阅下面的代码:

  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;
    }
  }
相关问题