2017-05-25 78 views
-1

這是我的第一個Java項目,所以我仍然在學習大量的東西以及如何應用它。替代變量?

我提出的隨機負載了創造者泰坦天降2使用

double b = (int)(Math.random() * 6 + 1); 

隨機生成許多針對以往變量i用於每個組的事我隨機。根據編號的不同,會顯示不同的文本,每個可能的遊戲選擇都有一個數字。我的問題是,既然你可以爲每個槍有兩個mod,那麼代碼中的兩個mod mod塊都是一樣的,但是如果偶然的話,兩個mod的隨機數都是相同的,它顯示的是同一個mod,這是你在遊戲中無法做到的。我想這樣做,如果b == a然後它重新隨機化b,但我不知道如何做到這一點。整個代碼將在底部,並顯示我想要避免的問題,這個問題發生在Gun mod 1和Gun mod 2,bluej syas中,當我試圖將它放在第二個if( b ==一)

 //Tacticals 
     double t = (int)(Math.random() * 7 + 1); 
     if (t == 1) { 
      System.out.println("Tactical: Cloak"); 
     } 
     if (t == 2) { 
      System.out.println("Tactical: Pulse Blade"); 
     } 
     if (t == 3) { 
      System.out.println("Tactical: Grappling Hook"); 
     } 
     if (t == 4) { 
      System.out.println("Tactical: Stim"); 
     } 
     if (t == 5) { 
      System.out.println("Tactical: A-Wall"); 
     } 
     if (t == 6) { 
      System.out.println("Tactical: Phase Shift"); 
     } 
     if (t == 7) { 
      System.out.println("Tactical: Holo Pilot"); 
     } 

     //Ordinance 
     double o = (int)(Math.random() * 6 + 1); 
     if (o == 1) { 
      System.out.println("Ordinance: Frag Grenade"); 
     } 
     if (o == 2) { 
      System.out.println("Ordinance: Arc Grenade"); 
     } 
     if (o == 3) { 
      System.out.println("Ordinance: Fire Star"); 
     } 
     if (o == 4) { 
      System.out.println("Ordinance: Gravity Star"); 
     } 
     if (o == 5) { 
      System.out.println("Ordinance: Electric Smoke Grenade"); 
     } 
     if (o == 6) { 
      System.out.println("Ordinance: Satchel Charge"); 
     } 

     //Primaries 
     double p = (int)(Math.random() * 22 + 1); 
     if (p == 1) { 
      System.out.println("Primary: R201"); 
     } 
     if (p == 2) { 
      System.out.println("Primary: R101"); 
     } 
     if (p == 3) { 
      System.out.println("Primary: G2"); 
     } 
     if (p == 4) { 
      System.out.println("Primary: Hemlock"); 
     } 
     if (p == 5) { 
      System.out.println("Primary: Flatline"); 
     } 
     if (p == 6) { 
      System.out.println("Primary: Alternator"); 
     } 
     if (p == 7) { 
      System.out.println("Primary: CAR"); 
     } 
     if (p == 8) { 
      System.out.println("Primary: R-97"); 
     } 
     if (p == 10) { 
      System.out.println("Primary: Volt"); 
     } 
     if (p == 11) { 
      System.out.println("Primary: L-STAR"); 
     } 
     if (p == 12) { 
      System.out.println("Primary: Spitfire"); 
     } 
     if (p == 13) { 
      System.out.println("Primary: Devotion"); 
     } 
     if (p == 14) { 
      System.out.println("Primary: Double Take"); 
     } 
     if (p == 15) { 
      System.out.println("Primary: Kraber"); 
     } 
     if (p == 16) { 
      System.out.println("Primary: DMR"); 
     } 
     if (p == 17) { 
      System.out.println("Primary: EVA-8"); 
     } 
     if (p == 18) { 
      System.out.println("Primary: Mastiff"); 
     } 
     if (p == 19) { 
      System.out.println("Primary: Cold War"); 
     } 
     if (p == 20) { 
      System.out.println("Primary: EPG"); 
     } 
     if (p == 21) { 
      System.out.println("Primary: Softball"); 
     } 
     if (p == 22) { 
      System.out.println("Primary: SMR"); 
     } 

     //Primay Gun Mod 1 
     double a = (int)(Math.random() * 6 + 1); 
     if (a == 1) { 
      System.out.println("Gun Mod 1: Extra Ammo"); 
     } 
     if (a == 2) { 
      System.out.println("Gun Mod 1: Speed Reload"); 
     } 
     if (a == 3) { 
      System.out.println("Gun Mod 1: Gunrunner"); 
     } 
     if (a == 4) { 
      System.out.println("Gun Mod 1: Gun Ready"); 
     } 
     if (a == 5) { 
      System.out.println("Gun Mod 1: Fast Swap"); 
     } 
     if (a == 6) { 
      System.out.println("Gun Mod 1: Tactikill"); 
     } 

     //Primary Gun Mod 2 
     double b = (int)(Math.random() * 6 + 1); 
     if (b ==a) { 
      double b = (int)(Math.random() * 6 + 1); 
     } 
     if (b == 1) { 
      System.out.println("Gun Mod 2: Extra Ammo"); 
     } 
     if (b== 2) { 
      System.out.println("Gun Mod 2: Speed Reload"); 
     } 
     if (b== 3) { 
      System.out.println("Gun Mod 2: Gunrunner"); 
     } 
     if (b== 4) { 
      System.out.println("Gun Mod 2: Gun Ready"); 
     } 
     if (b== 5) { 
      System.out.println("Gun Mod 2: Fast Swap"); 
     } 
     if (b== 6) { 
      System.out.println("Gun Mod 1: Tactikill"); 
     } 

     // Secondary 
     double s = (int)(Math.random() * 5 + 1); 
     if (s==1) { 
      System.out.println("Secondary: RE .45"); 
     } 
     if (s==2) { 
      System.out.println("Secondary: Hammond P2016"); 
     } 
     if (s==3) { 
      System.out.println("Secondary: Wingman Elite"); 
     } 
     if (s==4) { 
      System.out.println("Secondary: Mozambique "); 
     } 
     if (s==5) { 
      System.out.println("Secondary: Wingman B3"); 
     } 

     //Pilot Kit 1 
     double c = (int)(Math.random() * 4 +1); 
     if (c==1) { 
      System.out.println("Pilot Kit 1: Power Cell"); 
     } 
     if (c==2) { 
      System.out.println("Pilot Kit 1: Fast Regeneration"); 
     } 
     if (c==3) { 
      System.out.println("Pilot Kit 1: Ordinance Expert"); 
     } 
     if (c==4) { 
      System.out.println("Pilot Kit 1: Phase Embark"); 
     } 

     // Pilot Kit 2 
     double d = (int)(Math.random() * 4 + 1); 
     if (d==1) { 
      System.out.println("Pilot Kit 2: Wall Hang"); 
     } 
     if (d==2) { 
      System.out.println("Pilot Kit 2: Kill Report"); 
     } 
     if (d==3) { 
      System.out.println("Pilot Kit 2: Hover"); 
     } 
     if (d==4) { 
      System.out.println("Pilot Kit 2: Low Profile"); 
     } 
    } 
} 

enter image description here

+3

Java!= Javascript – Barmar

回答

1

據我瞭解,你要繼續做隨機的,直到你得到的值不等於前一個。

double t = (int)(Math.random() * 7 + 1); 
double o; 
do { 
    o = (int)(Math.random() * 6 + 1); 
} while(o == t); 
+0

這工作。謝謝。 –

+0

隨時。如果這有助於您解決問題,則可以將此標記爲已接受的答案。 – LIvanov