将else-if代码更改为更干净的代码

时间:2016-12-04 15:59:38

标签: java if-statement

我有跟随其他 - 如果代码,我真的很烦恼它所需的行的大小,所以我在想是否有更合适的方法来做到更干净,更快?< / p>

if (dialogResult == 0) {
            System.out.println("Yes option to try again!");
            if (level.equals(lp.LEVEL1.getLevelName())) {
                System.out.println(lp.LEVEL1.getLevelName());
                level = lp.LEVEL2.getLevelName(); // set level
                totalBall = lp.LEVEL2.getBallCount();
                numberOfExplodes = lp.LEVEL2.getBallExplosion();

            } else if (level.equals(lp.LEVEL2.getLevelName())) {
                System.out.println(lp.LEVEL2.getLevelName());
                level = lp.LEVEL3.getLevelName(); // set level
                totalBall = lp.LEVEL3.getBallCount();
                numberOfExplodes = lp.LEVEL3.getBallExplosion();

            } else if (level.equals(lp.LEVEL3.getLevelName())) {
                System.out.println(lp.LEVEL3.getLevelName());
                level = lp.LEVEL4.getLevelName(); // set level
                totalBall = lp.LEVEL4.getBallCount();
                numberOfExplodes = lp.LEVEL4.getBallExplosion();

            } else if (level.equals(lp.LEVEL4.getLevelName())) {
                System.out.println(lp.LEVEL4.getLevelName());
                level = lp.LEVEL5.getLevelName(); // set level
                totalBall = lp.LEVEL5.getBallCount();
                numberOfExplodes = lp.LEVEL5.getBallExplosion();

            } else if (level.equals(lp.LEVEL5.getLevelName())) {
                System.out.println(lp.LEVEL5.getLevelName());
                level = lp.LEVEL6.getLevelName(); // set level
                totalBall = lp.LEVEL6.getBallCount();
                numberOfExplodes = lp.LEVEL6.getBallExplosion();

            } else if (level.equals(lp.LEVEL6.getLevelName())) {
                System.out.println(lp.LEVEL6.getLevelName());
                level = lp.LEVEL7.getLevelName(); // set level
                totalBall = lp.LEVEL7.getBallCount();
                numberOfExplodes = lp.LEVEL7.getBallExplosion();

            } else if (level.equals(lp.LEVEL7.getLevelName())) {
                System.out.println(lp.LEVEL7.getLevelName());
                level = lp.LEVEL8.getLevelName(); // set level
                totalBall = lp.LEVEL8.getBallCount();
                numberOfExplodes = lp.LEVEL8.getBallExplosion();

            } else if (level.equals(lp.LEVEL8.getLevelName())) {
                System.out.println(lp.LEVEL8.getLevelName());
                level = lp.LEVEL9.getLevelName(); // set level
                totalBall = lp.LEVEL9.getBallCount();
                numberOfExplodes = lp.LEVEL9.getBallExplosion();

            }
            ...

我认为这里的转换可以顺利,但我也希望听到其他选择。

更新:这是我的类:lp是Balls类,它是枚举,并且在它的构造函数中有3个参数:

public enum BallCount {

    LEVEL1(5,1,"Level 1"),
    LEVEL2(10,2,"Level 2"),
    LEVEL3(15,4,"Level 3");

1 个答案:

答案 0 :(得分:0)

制作LEVEL1, LEVEL2, LEVEL3....或`lp&#39;实现以下方法:

/**
 * Gets Level enum from name.
 */    
public static Level getLevelFromName(String);

/**
 * Gets next level. 
 * Each level may either keeps reference to the next level or 
 * using enum cardinal number can get the next number.
 */
public Level getNextLevel();

然后你的if-then减少到没有条件的一个/两个衬里。

Level nextLevel = Level.getLevelFromName(level).getNextLevel();
level = lp.LEVEL6.getLevelName(); // set level
totalBall = lp.LEVEL6.getBallCount();
numberOfExplodes = lp.LEVEL6.getBallExplosion();