覆盖变量?

时间:2017-05-25 20:36:06

标签: java random

这是我的第一个java项目,所以我仍然在学习大量的东西以及如何应用它。

我正在为使用

的Titanfall 2制作一个随机加载的创建者
 double b = (int)(Math.random() * 6 + 1);

随机生成一个数字,对于我随机化的每组事物,我使用的变量。根据数字,将显示不同的文本,游戏中每个可能的选择一个数字。我的问题是,因为每个枪可以有两个mods代码中的枪模块是相同的但是如果偶然的两个mod的随机数相同它显示相同的mod,这是你在游戏中无法做到的。我想这样做,如果b == a然后它重新随机化b,但我无法弄清楚如何做到这一点。整个代码将在底部,并且图示显示我想要避免的情况,Gun mod 1和Gun mod 2的问题发生,bluej syas变量b已经定义,当我试图将它放在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

1 个答案:

答案 0 :(得分:1)

据我所知,你想继续随意,直到你得到一个与前一个不相等的值。

{{1}}