2016-03-27 36 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; 
    }