2016-03-27 65 views
0

我目前正試圖讓我的java程序打印出來不打印出重複的法律。該軟件是隨機挑選打印1至3法律,但我不想重複。我對java還是一個新手,到目前爲止只寫了一些程序。這是迄今爲止我寫的最複雜的一個。hashset刪除數組Java中的重複項

現在我想要軟件再次運行該隨機,如果它發現重複,直到打印中沒有重複。有沒有辦法做到這一點?

/** 
    *Seeds is a random number to pick which if statement will be used. 
    * telling the software how many laws to use during its random 
    * generation process 
    * 
    */ 
    Random seeds = new Random(); 
    seed = seeds.nextInt(3) + 1; 

    if (seed == 1){ 


     /**Beginning of random law selection for universe. Must find way 
     *Must find way to get rid of duplicate answers 
     */ 
     final String[] Laws = {"Standard Physics", "Magic", "Mad Science", 
      "Psionics", "Substandard Physics","Exotic"}; 
      Random random = new Random(); 
      int index = random.nextInt(Laws.length); 
      System.out.println("Law:" + Laws[index]); 
    } 

    else if (seed == 2) { 

     final String[] Laws = {"Standard Physics", "Magic", "Mad Science", 
      "Psionics", "Substandard Physics","Exotic"}; 
      Random random = new Random(); 
      int index = random.nextInt(Laws.length); 
      int index2 = random.nextInt(Laws.length); 
      System.out.println("Law:" + Laws[index] + "," + Laws[index2]); 

    } 

    else { 

     final String[] Laws = {"Standard Physics", "Magic", "Mad Science", 
      "Psionics", "Substandard Physics","Exotic"}; 
      Random random = new Random(); 
      int index = random.nextInt(Laws.length); 
      int index2 = random.nextInt(Laws.length); 
      int index3 = random.nextInt(Laws.length); 
      System.out.println("Law: " + Laws[index] + "," + Laws[index2] + 
        "," + Laws[index3]); 

    } 
+0

更少的代碼行,更少的問題,同一個String數組的3個聲明不好,聲明一次,如果其他塊 –

回答

2

想想這個問題的另一種方法是;您想要以隨機順序選擇值。這樣,除非一個元素出現多次,否則不會得到重複。

List<String> laws = Arrays.asList("Standard Physics", "Magic", "Mad Science", 
     "Psionics", "Substandard Physics","Exotic"); 
Collections.shuffle(laws); 
// select as many as you need 
List<String> lawsSelected = laws.subList(0, 3); 
0

這是一種方法。如果條件如此,則取3個布爾變量,並將其與每個if關聯一次,並在if被執行時將其設爲true。

Random seeds = new Random(); 
    seed = seeds.nextInt(3) + 1; 


boolean selected[] = new boolean[3]; 
selected[0] = selected[1] = selected[2] = false;//initially none of the if is executed 

    if (seed == 1 && !selected[seed-1]){//checking if not previously selected and "seed-1" since array indexes are from 0 to n-1 


     /**Beginning of random law selection for universe. Must find way 
     *Must find way to get rid of duplicate answers 
     */ 
     final String[] Laws = {"Standard Physics", "Magic", "Mad Science", 
      "Psionics", "Substandard Physics","Exotic"}; 
      Random random = new Random(); 
      int index = random.nextInt(Laws.length); 
      System.out.println("Law:" + Laws[index]); 
      selected[seed-1] = true; 
    } 

    else if (seed == 2 && !selected[seed-1]) { 

     final String[] Laws = {"Standard Physics", "Magic", "Mad Science", 
      "Psionics", "Substandard Physics","Exotic"}; 
      Random random = new Random(); 
      int index = random.nextInt(Laws.length); 
      int index2 = random.nextInt(Laws.length); 
      System.out.println("Law:" + Laws[index] + "," + Laws[index2]); 
      selected[seed-1] = true; 
    } 

    else if (!selected[seed-1]){ 

     final String[] Laws = {"Standard Physics", "Magic", "Mad Science", 
      "Psionics", "Substandard Physics","Exotic"}; 
      Random random = new Random(); 
      int index = random.nextInt(Laws.length); 
      int index2 = random.nextInt(Laws.length); 
      int index3 = random.nextInt(Laws.length); 
      System.out.println("Law: " + Laws[index] + "," + Laws[index2] + 
        "," + Laws[index3]); 
     selected[seed-1] = true; 
    }