2016-03-27 24 views
0

現在、私のJavaプログラムでは、印刷時に重複する法律を印刷しないようにしようとしています。ソフトウェアは1から3の法律を印刷するランダムなピッキングですが、重複は望ましくありません。私はまだまだJavaの新しいもので、これまでにいくつかのプログラムしか書いていません。これはこれまでに書いた中で最も複雑なものです。配列から重複を削除するためのハッシュセット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

同じ文字列配列の3つの宣言がうまくいかない場合は、他のブロックがある場合は一番上に一度宣言してください –

答えて

2

この問題を考えるもう1つの方法は、次のとおりです。ランダムな順序で値を選択したいとします。このようにして、要素が複数回表示されない限り、重複を取得しません。

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

これは1つのアプローチです。条件が3つのブール変数を取って3つのブール変数を1つずつ関連付けると、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; 
    } 
関連する問題