Android方法代码太大,该怎么办?

时间:2016-10-27 20:10:05

标签: android

人。我的一个方法代码出了问题,这很重要。代码编译,但是当需要使用该方法时,app关闭并显示错误。我不想重写我的代码。想分开它,但同样的问题。

制作另一种方法并在main方法中调用,但同样。

}
 checkpacks();
}

public void checkpacks(){
//Code

我知道我的代码很大,但由于这个问题仍未完成,代码会更大..

代码:

 public void getBoxes(View view) {
    final Context context = this;

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
            context);

    alertDialogBuilder.setTitle("Congratulations");

    alertDialogBuilder
            .setMessage("Message")
            .setCancelable(false)
            .setPositiveButton(getString(R.string.nice), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {

                }
            });

    AlertDialog BoxDialog = alertDialogBuilder.create();

    CarMain.main[2] += 1;
    CarMain.main[3] += 1;
    if (CarMain.main[2] == 5) {
        CarMain.main[2] -= 5;
        int PlayerLevel = CarMain.main[5];
        switch (PlayerLevel) {
            case 1:
                Pack.packs[0] = +1;
                break;
            case 2:
                Random PlayerLv2Generator = new Random();
                int PlayerLv2Int = PlayerLv2Generator.nextInt(2) + 1;
                switch (PlayerLv2Int) {
                    case 1:
                        Pack.packs[0] = +1;
                        break;
                    case 2:
                        Pack.packs[2] = +1;
                        break;
                }
                break;
            case 3:
                Random PlayerLv3Generator = new Random();
                int PlayerLv3Int = PlayerLv3Generator.nextInt(3) + 1;
                switch (PlayerLv3Int) {
                    case 1:
                        Pack.packs[0] = +1;
                        break;
                    case 2:
                        Pack.packs[2] = +1;
                        break;
                    case 3:
                        Pack.packs[4] = +1;
                        break;
                }
                break;
            case 4:
                Random PlayerLv4Generator = new Random();
                int PlayerLv4Int = PlayerLv4Generator.nextInt(4) + 1;
                switch (PlayerLv4Int) {
                    case 1:
                        Pack.packs[0] = +1;
                        break;
                    case 2:
                        Pack.packs[2] = +1;
                        break;
                    case 3:
                        Pack.packs[4] = +1;
                        break;
                    case 4:
                        Pack.packs[6] = +1;
                        break;
                }
                break;
            case 5:
                Random PlayerLv5Generator = new Random();
                int PlayerLv5Int = PlayerLv5Generator.nextInt(5) + 1;
                switch (PlayerLv5Int) {
                    case 1:
                        Pack.packs[0] = +1;
                        break;
                    case 2:
                        Pack.packs[2] = +1;
                        break;
                    case 3:
                        Pack.packs[4] = +1;
                        break;
                    case 4:
                        Pack.packs[6] = +1;
                        break;
                    case 5:
                        Pack.packs[8] = +1;
                        break;
                }
                break;
            case 6:
                Random PlayerLv6Generator = new Random();
                int PlayerLv6Int = PlayerLv6Generator.nextInt(6) + 1;
                switch (PlayerLv6Int) {
                    case 1:
                        Pack.packs[0] = +1;
                        break;
                    case 2:
                        Pack.packs[2] = +1;
                        break;
                    case 3:
                        Pack.packs[4] = +1;
                        break;
                    case 4:
                        Pack.packs[6] = +1;
                        break;
                    case 5:
                        Pack.packs[8] = +1;
                        break;
                    case 6:
                        Pack.packs[10] = +1;
                        break;
                }
                break;
            case 7:
                Random PlayerLv7Generator = new Random();
                int PlayerLv7Int = PlayerLv7Generator.nextInt(7) + 1;
                switch (PlayerLv7Int) {
                    case 1:
                        Pack.packs[0] = +1;
                        break;
                    case 2:
                        Pack.packs[2] = +1;
                        break;
                    case 3:
                        Pack.packs[4] = +1;
                        break;
                    case 4:
                        Pack.packs[6] = +1;
                        break;
                    case 5:
                        Pack.packs[8] = +1;
                        break;
                    case 6:
                        Pack.packs[10] = +1;
                        break;
                    case 7:
                        Pack.packs[12] = +1;
                        break;
                }
                break;
            case 8:
                Random PlayerLv8Generator = new Random();
                int PlayerLv8Int = PlayerLv8Generator.nextInt(8) + 1;
                switch (PlayerLv8Int) {
                    case 1:
                        Pack.packs[0] = +1;
                        break;
                    case 2:
                        Pack.packs[2] = +1;
                        break;
                    case 3:
                        Pack.packs[4] = +1;
                        break;
                    case 4:
                        Pack.packs[6] = +1;
                        break;
                    case 5:
                        Pack.packs[8] = +1;
                        break;
                    case 6:
                        Pack.packs[10] = +1;
                        break;
                    case 7:
                        Pack.packs[12] = +1;
                        break;
                    case 8:
                        Pack.packs[14] = +1;
                        break;
                }
                break;
            case 9:
                Random PlayerLv9Generator = new Random();
                int PlayerLv9Int = PlayerLv9Generator.nextInt(9) + 1;
                switch (PlayerLv9Int) {
                    case 1:
                        Pack.packs[0] = +1;
                        break;
                    case 2:
                        Pack.packs[2] = +1;
                        break;
                    case 3:
                        Pack.packs[4] = +1;
                        break;
                    case 4:
                        Pack.packs[6] = +1;
                        break;
                    case 5:
                        Pack.packs[8] = +1;
                        break;
                    case 6:
                        Pack.packs[10] = +1;
                        break;
                    case 7:
                        Pack.packs[12] = +1;
                        break;
                    case 8:
                        Pack.packs[14] = +1;
                        break;
                    case 9:
                        Pack.packs[16] = +1;
                        break;
                }
                break;
            case 10:
                Random PlayerLv10Generator = new Random();
                int PlayerLv10Int = PlayerLv10Generator.nextInt(10) + 1;
                switch (PlayerLv10Int) {
                    case 1:
                        Pack.packs[0] = +1;
                        break;
                    case 2:
                        Pack.packs[2] = +1;
                        break;
                    case 3:
                        Pack.packs[4] = +1;
                        break;
                    case 4:
                        Pack.packs[6] = +1;
                        break;
                    case 5:
                        Pack.packs[8] = +1;
                        break;
                    case 6:
                        Pack.packs[10] = +1;
                        break;
                    case 7:
                        Pack.packs[12] = +1;
                        break;
                    case 8:
                        Pack.packs[14] = +1;
                        break;
                    case 9:
                        Pack.packs[16] = +1;
                        break;
                    case 10:
                        Pack.packs[18] = +1;
                        break;
                }
        }
    }
    if (Pack.packs[0] == 1) {
        Pack.packs[0] -= 1;
        Random PackLv1Generator = new Random();
        int PackLv1Int = PackLv1Generator.nextInt(12) + 1;
        switch (PackLv1Int) {
            case 1:
                Box.boxes[0] += 1; // Accessories Pack 1
                BoxDialog.setMessage("You have got " + getString(R.string.Accessories_Pack_1));
                BoxDialog.show();
                break;
            case 2:
                Box.boxes[40] += 1; // Body Pack 1
                BoxDialog.setMessage("You have got " + getString(R.string.Body_Pack_1));
                BoxDialog.show();
                break;
            case 3:
                Box.boxes[80] += 1; // Brakes Pack 1
                BoxDialog.setMessage("You have got " + getString(R.string.Brakes_Pack_1));
                BoxDialog.show();
                break;
            case 4:
                Box.boxes[120] += 1; // Cooling Pack 1
                BoxDialog.setMessage("You have got " + getString(R.string.Cooling_Pack_1));
                BoxDialog.show();
                break;
            case 5:
                Box.boxes[160] += 1; // Electronics Pack 1
                BoxDialog.setMessage("You have got " + getString(R.string.Electronics_Pack_1));
                BoxDialog.show();
                break;
            case 6:
                Box.boxes[200] += 1; // Engine Pack 1
                BoxDialog.setMessage("You have got " + getString(R.string.Engine_Pack_1));
                BoxDialog.show();
                break;
            case 7:
                Box.boxes[240] += 1; // Exhaust Pack 1
                BoxDialog.setMessage("You have got " + getString(R.string.Exhaust_Pack_1));
                BoxDialog.show();
                break;
            case 8:
                Box.boxes[280] += 1; // Fuel Pack 1
                BoxDialog.setMessage("You have got " + getString(R.string.Fuel_Pack_1));
                BoxDialog.show();
                break;
            case 9:
                Box.boxes[320] += 1; // Steering Pack 1
                BoxDialog.setMessage("You have got " + getString(R.string.Steering_Pack_1));
                BoxDialog.show();
                break;
            case 10:
                Box.boxes[360] += 1; // Suspension Pack 1
                BoxDialog.setMessage("You have got " + getString(R.string.Suspension_Pack_1));
                BoxDialog.show();
                break;
            case 11:
                Box.boxes[400] += 1; // Transmission Pack 1
                BoxDialog.setMessage("You have got " + getString(R.string.Transmission_Pack_1));
                BoxDialog.show();
                break;
            case 12:
                Box.boxes[440] += 1; // Tires Pack 1
                BoxDialog.setMessage("You have got " + getString(R.string.Tires_Pack_1));
                BoxDialog.show();
                break;
        }
    }
    if (Pack.packs[2] == 1) {
        Pack.packs[2] -= 1;
        Random PackLv2Generator = new Random();
        int PackLv2Int = PackLv2Generator.nextInt(12) + 1;
        switch (PackLv2Int) {
            case 1:
                Box.boxes[4] += 1; // Accessories Pack 2
                BoxDialog.setMessage("You have got " + getString(R.string.Accessories_Pack_2));
                BoxDialog.show();
                break;
            case 2:
                Box.boxes[44] += 1; // Body Pack 2
                BoxDialog.setMessage("You have got " + getString(R.string.Body_Pack_2));
                BoxDialog.show();
                break;
            case 3:
                Box.boxes[84] += 1; // Brakes Pack 2
                break;
            case 4:
                Box.boxes[124] += 1; // Cooling Pack 2
                break;
            case 5:
                Box.boxes[164] += 1; // Electronics Pack 2
                break;
            case 6:
                Box.boxes[204] += 1; // Engine Pack 2
                break;
            case 7:
                Box.boxes[244] += 1; // Exhaust Pack 2
                break;
            case 8:
                Box.boxes[284] += 1; // Fuel Pack 2
                break;
            case 9:
                Box.boxes[324] += 1; // Steering Pack 2
                break;
            case 10:
                Box.boxes[364] += 1; // Suspension Pack 2
                break;
            case 11:
                Box.boxes[404] += 1; // Transmission Pack 2
                break;
            case 12:
                Box.boxes[444] += 1; // Tires Pack 2
                break;
        }
    }
    if (Pack.packs[4] == 1) {
        Pack.packs[4] -= 1;
        Random PackLv3Generator = new Random();
        int PackLv3Int = PackLv3Generator.nextInt(12) + 1;
        switch (PackLv3Int) {
            case 1:
                Box.boxes[8] += 1; // Accessories Pack 3
                BoxDialog.setMessage("You have got " + getString(R.string.Accessories_Pack_3));
                BoxDialog.show();
                break;
            case 2:
                Box.boxes[48] += 1; // Body Pack 3
                BoxDialog.setMessage("You have got " + getString(R.string.Body_Pack_3));
                BoxDialog.show();
                break;
            case 3:
                Box.boxes[88] += 1; // Brakes Pack 3
                break;
            case 4:
                Box.boxes[128] += 1; // Cooling Pack 3
                break;
            case 5:
                Box.boxes[168] += 1; // Electronics Pack 3
                break;
            case 6:
                Box.boxes[208] += 1; // Engine Pack 3
                break;
            case 7:
                Box.boxes[248] += 1; // Exhaust Pack 3
                break;
            case 8:
                Box.boxes[288] += 1; // Fuel Pack 3
                break;
            case 9:
                Box.boxes[328] += 1; // Steering Pack 3
                break;
            case 10:
                Box.boxes[368] += 1; // Suspension Pack 3
                break;
            case 11:
                Box.boxes[408] += 1; // Transmission Pack 3
                break;
            case 12:
                Box.boxes[448] += 1; // Tires Pack 3
                break;
        }
    }
    if (Pack.packs[6] == 1) {
        Pack.packs[6] -= 1;
        Random PackLv4Generator = new Random();
        int PackLv4Int = PackLv4Generator.nextInt(12) + 1;
        switch (PackLv4Int) {
            case 1:
                Box.boxes[12] += 1; // Accessories Pack 4
                BoxDialog.setMessage("You have got " + getString(R.string.Accessories_Pack_4));
                BoxDialog.show();
                break;
            case 2:
                Box.boxes[52] += 1; // Body Pack 4
                BoxDialog.setMessage("You have got " + getString(R.string.Body_Pack_4));
                BoxDialog.show();
                break;
            case 3:
                Box.boxes[92] += 1; // Brakes Pack 4
                break;
            case 4:
                Box.boxes[132] += 1; // Cooling Pack 4
                break;
            case 5:
                Box.boxes[172] += 1; // Electronics Pack 4
                break;
            case 6:
                Box.boxes[212] += 1; // Engine Pack 4
                break;
            case 7:
                Box.boxes[252] += 1; // Exhaust Pack 4
                break;
            case 8:
                Box.boxes[292] += 1; // Fuel Pack 4
                break;
            case 9:
                Box.boxes[332] += 1; // Steering Pack 4
                break;
            case 10:
                Box.boxes[372] += 1; // Suspension Pack 4
                break;
            case 11:
                Box.boxes[412] += 1; // Transmission Pack 4
                break;
            case 12:
                Box.boxes[452] += 1; // Tires Pack 4
                break;
        }
    }
    if (Pack.packs[8] == 1) {
        Pack.packs[8] -= 1;
        Random PackLv5Generator = new Random();
        int PackLv5Int = PackLv5Generator.nextInt(12) + 1;
        switch (PackLv5Int) {
            case 1:
                Box.boxes[16] += 1; // Accessories Pack 5
                BoxDialog.setMessage("You have got " + getString(R.string.Accessories_Pack_5));
                BoxDialog.show();
                break;
            case 2:
                Box.boxes[56] += 1; // Body Pack 5
                BoxDialog.setMessage("You have got " + getString(R.string.Body_Pack_5));
                BoxDialog.show();
                break;
            case 3:
                Box.boxes[96] += 1; // Brakes Pack 5
                break;
            case 4:
                Box.boxes[136] += 1; // Cooling Pack 5
                break;
            case 5:
                Box.boxes[176] += 1; // Electronics Pack 5
                break;
            case 6:
                Box.boxes[216] += 1; // Engine Pack 5
                break;
            case 7:
                Box.boxes[256] += 1; // Exhaust Pack 5
                break;
            case 8:
                Box.boxes[296] += 1; // Fuel Pack 5
                break;
            case 9:
                Box.boxes[336] += 1; // Steering Pack 5
                break;
            case 10:
                Box.boxes[376] += 1; // Suspension Pack 5
                break;
            case 11:
                Box.boxes[416] += 1; // Transmission Pack 5
                break;
            case 12:
                Box.boxes[456] += 1; // Tires Pack 5
                break;
        }
    }
    if (Pack.packs[10] == 1) {
        Pack.packs[10] -= 1;
        Random PackLv6Generator = new Random();
        int PackLv6Int = PackLv6Generator.nextInt(12) + 1;
        switch (PackLv6Int) {
            case 1:
                Box.boxes[20] += 1; // Accessories Pack 6
                BoxDialog.setMessage("You have got " + getString(R.string.Accessories_Pack_6));
                BoxDialog.show();
                break;
            case 2:
                Box.boxes[60] += 1; // Body Pack 6
                BoxDialog.setMessage("You have got " + getString(R.string.Body_Pack_6));
                BoxDialog.show();
                break;
            case 3:
                Box.boxes[100] += 1; // Brakes Pack 6
                break;
            case 4:
                Box.boxes[140] += 1; // Cooling Pack 6
                break;
            case 5:
                Box.boxes[180] += 1; // Electronics Pack 6
                break;
            case 6:
                Box.boxes[220] += 1; // Engine Pack 6
                break;
            case 7:
                Box.boxes[260] += 1; // Exhaust Pack 6
                break;
            case 8:
                Box.boxes[300] += 1; // Fuel Pack 6
                break;
            case 9:
                Box.boxes[340] += 1; // Steering Pack 6
                break;
            case 10:
                Box.boxes[380] += 1; // Suspension Pack 6
                break;
            case 11:
                Box.boxes[420] += 1; // Transmission Pack 6
                break;
            case 12:
                Box.boxes[460] += 1; // Tires Pack 6
                break;
        }
    }
    if (Pack.packs[12] == 1) {
        Pack.packs[12] -= 1;
        Random PackLv7Generator = new Random();
        int PackLv7Int = PackLv7Generator.nextInt(12) + 1;
        switch (PackLv7Int) {
            case 1:
                Box.boxes[24] += 1; // Accessories Pack 7
                BoxDialog.setMessage("You have got " + getString(R.string.Accessories_Pack_7));
                BoxDialog.show();
                break;
            case 2:
                Box.boxes[64] += 1; // Body Pack 7
                BoxDialog.setMessage("You have got " + getString(R.string.Body_Pack_7));
                BoxDialog.show();
                break;
            case 3:
                Box.boxes[104] += 1; // Brakes Pack 7
                break;
            case 4:
                Box.boxes[144] += 1; // Cooling Pack 7
                break;
            case 5:
                Box.boxes[184] += 1; // Electronics Pack 7
                break;
            case 6:
                Box.boxes[224] += 1; // Engine Pack 7
                break;
            case 7:
                Box.boxes[264] += 1; // Exhaust Pack 7
                break;
            case 8:
                Box.boxes[304] += 1; // Fuel Pack 7
                break;
            case 9:
                Box.boxes[344] += 1; // Steering Pack 7
                break;
            case 10:
                Box.boxes[384] += 1; // Suspension Pack 7
                break;
            case 11:
                Box.boxes[424] += 1; // Transmission Pack 7
                break;
            case 12:
                Box.boxes[464] += 1; // Tires Pack 7
                break;
        }
    }
    if (Pack.packs[14] == 1) {
        Pack.packs[14] -= 1;
        Random PackLv8Generator = new Random();
        int PackLv8Int = PackLv8Generator.nextInt(12) + 1;
        switch (PackLv8Int) {
            case 1:
                Box.boxes[28] += 1; // Accessories Pack 8
                BoxDialog.setMessage("You have got " + getString(R.string.Accessories_Pack_8));
                BoxDialog.show();
                break;
            case 2:
                Box.boxes[68] += 1; // Body Pack 8
                BoxDialog.setMessage("You have got " + getString(R.string.Body_Pack_8));
                BoxDialog.show();
                break;
            case 3:
                Box.boxes[108] += 1; // Brakes Pack 8
                break;
            case 4:
                Box.boxes[148] += 1; // Cooling Pack 8
                break;
            case 5:
                Box.boxes[188] += 1; // Electronics Pack 8
                break;
            case 6:
                Box.boxes[228] += 1; // Engine Pack 8
                break;
            case 7:
                Box.boxes[268] += 1; // Exhaust Pack 8
                break;
            case 8:
                Box.boxes[308] += 1; // Fuel Pack 8
                break;
            case 9:
                Box.boxes[348] += 1; // Steering Pack 8
                break;
            case 10:
                Box.boxes[388] += 1; // Suspension Pack 8
                break;
            case 11:
                Box.boxes[428] += 1; // Transmission Pack 8
                break;
            case 12:
                Box.boxes[468] += 1; // Tires Pack 8
                break;
        }
    }
    if (Pack.packs[16] == 1) {
        Pack.packs[16] -= 1;
        Random PackLv9Generator = new Random();
        int PackLv9Int = PackLv9Generator.nextInt(12) + 1;
        switch (PackLv9Int) {
            case 1:
                Box.boxes[32] += 1; // Accessories Pack 9
                BoxDialog.setMessage("You have got " + getString(R.string.Accessories_Pack_9));
                BoxDialog.show();
                break;
            case 2:
                Box.boxes[72] += 1; // Body Pack 9
                BoxDialog.setMessage("You have got " + getString(R.string.Body_Pack_9));
                BoxDialog.show();
                break;
            case 3:
                Box.boxes[112] += 1; // Brakes Pack 9
                break;
            case 4:
                Box.boxes[152] += 1; // Cooling Pack 9
                break;
            case 5:
                Box.boxes[192] += 1; // Electronics Pack 9
                break;
            case 6:
                Box.boxes[232] += 1; // Engine Pack 9
                break;
            case 7:
                Box.boxes[272] += 1; // Exhaust Pack 9
                break;
            case 8:
                Box.boxes[312] += 1; // Fuel Pack 9
                break;
            case 9:
                Box.boxes[352] += 1; // Steering Pack 9
                break;
            case 10:
                Box.boxes[392] += 1; // Suspension Pack 9
                break;
            case 11:
                Box.boxes[432] += 1; // Transmission Pack 9
                break;
            case 12:
                Box.boxes[472] += 1; // Tires Pack 9
                break;
        }        
        }
    }
}

1 个答案:

答案 0 :(得分:1)

以下是如何缩小代码大小的示例:

您的switch (PlayerLevel) {可以缩减为:

Random gen = new Random();
int rndValue = gen.nextInt(PlayerLevel) + 1;
switch (rndValue) {
    case 1:
        Pack.packs[0] = +1;
        break;
    case 2:
        Pack.packs[2] = +1;
        break;
    case 3:
        Pack.packs[4] = +1;
        break;
    case 4:
        Pack.packs[6] = +1;
        break;
    case 5:
        Pack.packs[8] = +1;
        break;
    case 6:
        Pack.packs[10] = +1;
        break;
    case 7:
        Pack.packs[12] = +1;
        break;
    case 8:
        Pack.packs[14] = +1;
        break;
    case 9:
        Pack.packs[16] = +1;
        break;
    case 10:
        Pack.packs[18] = +1;
        break;
}

您可以看到,可以替换为

Random gen = new Random();
int rndValue = gen.nextInt(PlayerLevel);
Pack.packs[rndValue / 2] += 1;

这涵盖了整个10个级别,因为随机生成的方式。 (顺便说一句,你在奇数指数中放了什么?)

然后,你真的只需要一个Random。您可以创建一次并在任何地方使用它,无需在每次需要时重新创建它。

另外,请使用对象和属性而不是带有注释的大数组。

考虑更改模型,并替换例如

Box.boxes[280] += 1; // Fuel Pack 1

Box.fuelPack[0] += 1;

(快速问题,为什么你的指数与4完全相同?除了2的包?)

修改

看起来你正在递增packs[n]只是为了减少它。也许你可以改为使用变量:

Random gen = new Random();
int pack = gen.nextInt(PlayerLevel);
// Then switch on that value.

然后,所有packs结果都会产生类似int PackLv3Int = PackLv3Generator.nextInt(12) + 1;

的结果

您只能执行一次:int packLvl = gen.nextInt(12);

然后你真正增加的是

Box.boxes[pack * 4 + packLvl * 40];

如果您总是打电话给BoxDialog.show();,请在结束时拨打一次。

更多编辑

以下是您可以用几行重写方法的方法:

String[] labels = new String {"Accessories", "Body", "Brakes", "Cooling", "Electronics", "Engine", "Exhaust", "Fuel", "Steering", "Suspensions", "Transmission", "Tires"};
Random gen = new Random();

int pack = gen.nextInt(PlayerLevel);
int packLvl = gen.nextInt(labels.length);

Box.boxes[pack * 4 + packLvl * 40];

String packageName = getPackageName();
int resId = getResources().getIdentifier(aString, labels[pack] + "_Pack_" + (packLvl + 1), packageName);
BoxDialog.setMessage("You have got " + getString(resId));
BoxDialog.show();