2017-05-25 6 views
-1

Dies ist mein erstes Java-Projekt, also lerne ich immer noch eine Menge und wie man es anwendet.Variablen überschreiben?

Ich mache eine zufällige Belastung aus Schöpfer für Titanfall 2, die

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

nach dem Zufallsprinzip eine Zahl zu erzeugen, für die je Variable i für jeden Satz von Dingen verwenden nutzt ich randomisieren. Basierend auf der Nummer wird ein anderer Text angezeigt, eine Nummer für jede mögliche Auswahl im Spiel. Mein Problem ist, dass, da man für jede Waffe zwei Mods haben kann, beide Gun Mod Blöcke im Code identisch sind, aber wenn zufällig beide Zufallszahlen für die Mods gleich sind, zeigt es den gleichen Mod, was man im Spiel nicht machen kann. Ich würde es gerne so machen, wenn b == a dann re-randomisiert b, aber ich kann nicht herausfinden, wie das geht. Der gesamte Code wird unten sein und ein Bild zeigen, was ich vermeiden möchte, das Problem tritt mit Gun mod 1 und Gun mod 2, bluej syas, dass Variable b ist bereits definiert, wenn ich versuchte, es die zweite in der If (b == a)

 //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

Antwort

1

Soweit ich verstehe Sie zufällig behalten wollen tun, bis Sie einen Wert zu erhalten, die nicht gleich den vorherigen.

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

Dies funktionierte. Vielen Dank. –

+0

Jederzeit. Wenn Sie dadurch Ihr Problem lösen konnten, können Sie dies als akzeptierte Antwort markieren. – LIvanov

Verwandte Themen