2013-12-14 99 views
-1

嘿家伙即时制作游戏,以帮助我学习决赛。它基本上是闪存卡。我希望程序通过随机选择一个打印出我的问题和答案。事情是我不希望他相同的随机数被挑选两次。我如何确保相同的号码不被挑选两次,直到我没有问题?我试图将每个随机数存储在黑名单中,以便在程序打印之前检查数字是否已经存在。如何让我的程序继续?

import java.util.ArrayList; 
    import java.util.Random; 

    public class Games { 

     static ArrayList<Integer> blacklist = new ArrayList<Integer>(); 
     static int number; 
     static String[][] yes = {{"Apostolic Orgin", "Comes form the apostles"}, 
      {"Biblical inerrancy", "the doctrine that the books are free from error reading the truth"}, 
      {"Divine Inspiration", "the assistance the holy spirit gave the authors or the bible so they could write"}, 
      {"fundamentalist approach", "interpretation of the bible and christian doctrine based on the literal meaning og bible's word"}, {"pentateuch", "first 5 books of old testament"}}; 

     public static void main(String[] args) { 
      Printer(pickQuestion(), pickAnswer()); 
     } 

     public static int RandomNumber(int i) { 
      int c; 
      Random r = new Random(); 
      c = r.nextInt(i); 
      return c; 

     } 

     public static void Printer(String question, String answer) { 
      System.out.println("Question: " + question); 
      System.out.println("Answer: " + answer); 
     } 

     public static String pickQuestion() { 
      number = RandomNumber(yes.length); 
      return yes[number][0]; 
     } 

     public static String pickAnswer() { 
      return yes[number][1]; 
     } 
} 

回答

0

最简单的方法是预先洗牌的问题列表,或者与问题搞乱和直接回答阵列,或通过从1创建一个整数数组到n,其中是的问题混洗该号码, ,然后将“卡”加载到数组中指向该值的位置。 即,创建 [1,2,3,4,5] 随机: [5,1,3,4,2] 和负载问题5,则第1个问题,等等

这后一种选项会使一些事情更容易,尤其是调试!

按顺序创建数组对于循环来说是微不足道的,并且有很多用于混洗的标准算法。 Random shuffling of an array

相关问题