2013-12-14 68 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

相關問題