Java Loop不会像我想的那样工作

时间:2016-03-15 10:34:27

标签: java

很抱歉,如果这是一个简单的问题而且这是我的错,但我无法弄清楚自己的循环 - 它只能运行一次。目前,循环一次构建所有20辆汽车,但我想每次点击只建一辆汽车。希望你能理解我。

我的循环代码:

for (int j = 3;j <= 80; j+=4){
    if(CarsLv1.cars[j] == 0){
        for(int i=1;i <= 25;i+=2){
            Part.parts[i] -=1;
        }   
        //CarFrame.frames[1] -=1;                               
        CarsLv1.cars[j] +=1;

        switch(j){

            case 3:{
                JOptionPane.showMessageDialog (null, "You have gained Aston Martin DB9!", "Congratulations", JOptionPane.INFORMATION_MESSAGE, ImagesHolder.Aston_Martin_DB9Icon);
                break;}
            case 7:{
                JOptionPane.showMessageDialog (null, "You have gained Acura NSX!", "Congratulations", JOptionPane.INFORMATION_MESSAGE, ImagesHolder.Acura_NSXIcon);
                break;}
            case 11:{
                JOptionPane.showMessageDialog (null, "You have gained Ford Mustang!", "Congratulations", JOptionPane.INFORMATION_MESSAGE, ImagesHolder.Ford_MustangIcon);
                break;
            }
            case 15:{
                JOptionPane.showMessageDialog (null, "You have gained Moskvich 412!", "Congratulations", JOptionPane.INFORMATION_MESSAGE, ImagesHolder.Moskvich_412Icon);
                break;
            }
            case 19:{
                JOptionPane.showMessageDialog (null, "You have gained Kia Venga!", "Congratulations", JOptionPane.INFORMATION_MESSAGE, ImagesHolder.Kia_VengaIcon);
                break;
            }
            case 23:{
                JOptionPane.showMessageDialog (null, "You have gained Fiat 1500!", "Congratulations", JOptionPane.INFORMATION_MESSAGE, ImagesHolder.Fiat_1500Icon);
                break;
            }
            case 27:{
                JOptionPane.showMessageDialog (null, "You have gained Ferrari Enzo!", "Congratulations", JOptionPane.INFORMATION_MESSAGE, ImagesHolder.Ferrari_EnzoIcon);
                break;
            }
            case 31:{
                JOptionPane.showMessageDialog (null, "You have gained Aston Martin Rapide!", "Congratulations", JOptionPane.INFORMATION_MESSAGE, ImagesHolder.Aston_Martin_RapideIcon);
                break;
            }
            case 35:{
                JOptionPane.showMessageDialog (null, "You have gained Koenigsegg CCX!", "Congratulations", JOptionPane.INFORMATION_MESSAGE, ImagesHolder.Koenigsegg_CCXIcon);
                break;
            }
            case 39:{
                JOptionPane.showMessageDialog (null, "You have gained Honda Civic!", "Congratulations", JOptionPane.INFORMATION_MESSAGE, ImagesHolder.Honda_CivicIcon);
                break;
            }
            case 43:{
                JOptionPane.showMessageDialog (null, "You have gained Cadilliac ATS!", "Congratulations", JOptionPane.INFORMATION_MESSAGE, ImagesHolder.Cadilliac_ATSIcon);
                break;
            }
            case 47:{
                JOptionPane.showMessageDialog (null, "You have gained Mitsubishi Lancer Evolution X!", "Congratulations", JOptionPane.INFORMATION_MESSAGE,ImagesHolder.Mitsubishi_LancerEvolutionXIcon);
                break;
            }
            case 51:{
                JOptionPane.showMessageDialog (null, "You have gained Infiniti FX!", "Congratulations", JOptionPane.INFORMATION_MESSAGE, ImagesHolder.Infiniti_FXIcon);
                break;
            }
            case 55:{
                JOptionPane.showMessageDialog (null, "You have gained Lancia Thema!", "Congratulations", JOptionPane.INFORMATION_MESSAGE, ImagesHolder.Lancia_ThemaIcon);
                break;
            }
            case 59:{
                JOptionPane.showMessageDialog (null, "You have gained Volvo XC90!", "Congratulations", JOptionPane.INFORMATION_MESSAGE, ImagesHolder.Volvo_XC90Icon);
                break;
            }
            case 63:{
                JOptionPane.showMessageDialog (null, "You have gained Acura CSX!", "Congratulations", JOptionPane.INFORMATION_MESSAGE, ImagesHolder.Acura_CSXIcon);
                break;
            }
            case 67:{
                JOptionPane.showMessageDialog (null, "You have gained Saab Sonett II!", "Congratulations", JOptionPane.INFORMATION_MESSAGE, ImagesHolder.Saab_SonettIIIcon);
                break;
            }
            case 71:{
                JOptionPane.showMessageDialog (null, "You have gained Renault Floride!", "Congratulations", JOptionPane.INFORMATION_MESSAGE, ImagesHolder.Renault_FlorideIcon);
                break;
            }
            case 75:{
                JOptionPane.showMessageDialog (null, "You have gained BMW e34!", "Congratulations", JOptionPane.INFORMATION_MESSAGE, ImagesHolder.BMW_e34Icon);
                break;
            }
            case 79:{
                JOptionPane.showMessageDialog (null, "You have gained Chrysler Cordoba!", "Congratulations", JOptionPane.INFORMATION_MESSAGE, ImagesHolder.Chrysler_CordobaIcon);                                   
                JOptionPane.showMessageDialog(null, "You have collected all LVL 1 cars", "Congratulations", JOptionPane.INFORMATION_MESSAGE, ImagesHolder.MaxUpgradeIcon);
                break;
            }
            default:{
                JOptionPane.showMessageDialog(null, "You have collected all LVL 1 cars", "Congratulations", JOptionPane.INFORMATION_MESSAGE, ImagesHolder.MaxUpgradeIcon);
                break;
            }
        }
    }
}

2 个答案:

答案 0 :(得分:2)

switch声明之后休息一下。 break语句中的switch只会破坏特定情况,它们不会退出周围的循环。

for (int j = 3;j <= 80; j+=4){
  if (CarsLv1.cars[j] == 0) {
    switch(j){
      case 3:
        // Whatever.
        break;  // This breaks the case.
    }
    break;  // This breaks the loop;
  }
}

注意,如果您定义了代表汽车的枚举,那将会更加清晰,例如:

enum Car {
  ASTON_MARTIN_DB9(3, "Aston Martin DB9", ImagesHolder.Aston_Martin_DB9Icon),
  ACURA_NSX(7, "Acura NSX", ImagesHolder.Acura_NSXIcon),
  // etc, for other cars.
  ;

  private final int number;
  private final String name;
  private final ImageIcon icon;

  private Car(int number, String name, ImageIcon icon) {
    // Assign parameters to fields.
  }

  // Add getters.
}

然后,您可以自行遍历汽车,而不是遍历这些数字:

boolean found = false;
for (Car car : Car.values()) {
  int j = car.getNumber();
  if(CarsLv1.cars[j] != 0) continue;

  for(int i=1;i <= 25;i+=2){
    Part.parts[i] -=1;
  }                                
  CarsLv1.cars[j] +=1;
  JOptionPane.showMessageDialog (null, "You have gained " + car.getName() + "!", "Congratulations", JOptionPane.INFORMATION_MESSAGE, car.getIcon());
  found = true;
  break;
}
if (!found) {
  // Show the message about having collected all cars.
}

即。避免需要这个伟大的大开关,以及所有重复的代码。

答案 1 :(得分:2)

  

循环一次构建所有20辆汽车,但我想每次点击只建一辆汽车。

提示:如果您希望代码只执行一次,而不是连续多次执行。也许你不应该使用循环。

创建方法&#34; createCar&#34; (或类似的东西)并在你做&#34;点击&#34;。

时调用该方法