2017-05-25 102 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