将if语句组合成1个switch语句

时间:2014-04-10 20:17:54

标签: java swing

此KeyListener中的这4个if语句检查输入了什么箭头键。我想通过将4个if语句组合成更简洁的switch语句或条件语句来提高效率。

如何将4个if语句组合成1个条件语句或switch语句?

class MyKeyListener implements KeyListener {

    public void keyReleased(KeyEvent e) { }

    public void keyTyped(KeyEvent e) {}


    public void keyPressed(KeyEvent e) {

        int keyCode = e.getKeyCode();



        for (int i=0; i < a.length;i++){
            for (int k=0; k < a[i].length;k++){
                Contents tileLeft = lr.getTile(levelData, i-1, k);
                Contents tileUp = lr.getTile(levelDate, i, k+1);
                Contents tileRight = lr.getTile(levelDate, i+1, k);
                Contents tileDown = lr.getTile(levelData, i, k-1);


                if(keyCode == KeyEvent.VK_LEFT) {     

                    if (tileLeft == Contents.EMPTY || tileLeft == Contents.GOAL) {
                        levelData[i][k] = Contents.EMPTY;
                        levelData[i-1][k] = Contents.PLAYER;                              
                    }
                }

                if(keyCode == KeyEvent.VK_UP) {


                    if (tileUp == Contents.EMPTY || tileUp == Contents.GOAL) {
                        levelData[i][k] = Contents.EMPTY;
                        levelData[i][k+1] = Contents.PLAYER;                              
                    }
                }

                if(keyCode == KeyEvent.VK_RIGHT) {

                    if (tileRight == Contents.EMPTY || tileRight == Contents.GOAL) {
                        levelData[i][k] = Contents.EMPTY;
                        levelData[i+1][k] = Contents.PLAYER;                              
                    }

                }

                if(keyCode == KeyEvent.VK_DOWN) {


                    if (tileDown == Contents.EMPTY || tileDown == Contents.GOAL) {
                        levelData[i][k] = Contents.EMPTY;
                        levelData[i][k-1] = Contents.PLAYER;                              
                    }
                }


            }
        }
    }

}

1 个答案:

答案 0 :(得分:0)

你可以尝试使用else if将所有语句组合成一个或使用开关。

E.g。

//Use else if and define the condition like you normally do for an if statement
            if(keyCode == KeyEvent.VK_LEFT) {

                if (tileLeft == Contents.EMPTY || tileLeft == Contents.GOAL) {
                    levelData[i][k] = Contents.EMPTY;
                    levelData[i-1][k] = Contents.PLAYER;
                }

            } else if(keyCode == KeyEvent.VK_UP) {

                if (tileUp == Contents.EMPTY || tileUp == Contents.GOAL) {
                    levelData[i][k] = Contents.EMPTY;
                    levelData[i][k+1] = Contents.PLAYER;
                }

            }else if(keyCode == KeyEvent.VK_RIGHT) {

                if (tileRight == Contents.EMPTY || tileRight == Contents.GOAL) {
                    levelData[i][k] = Contents.EMPTY;
                    levelData[i+1][k] = Contents.PLAYER;
                }

            }else if(keyCode == KeyEvent.VK_DOWN) {

                if (tileDown == Contents.EMPTY || tileDown == Contents.GOAL) {
                    levelData[i][k] = Contents.EMPTY;
                    levelData[i][k-1] = Contents.PLAYER;
                }

            }

            /**
             * Alternatively you could use a switch case statement
             * The same principle with the if statement above,  will give you the same result
             */
            switch (keyCode) {

                case KeyEvent.VK_LEFT:

                    if (tileLeft == Contents.EMPTY || tileLeft == Contents.GOAL) {
                        levelData[i][k] = Contents.EMPTY;
                        levelData[i-1][k] = Contents.PLAYER;
                    }

                    break;

                case KeyEvent.VK_UP:

                    if (tileUp == Contents.EMPTY || tileUp == Contents.GOAL) {
                        levelData[i][k] = Contents.EMPTY;
                        levelData[i][k+1] = Contents.PLAYER;
                    }

                    break;

                case KeyEvent.VK_RIGHT:

                    if (tileRight == Contents.EMPTY || tileRight == Contents.GOAL) {
                        levelData[i][k] = Contents.EMPTY;
                        levelData[i+1][k] = Contents.PLAYER;
                    }

                    break;

                case KeyEvent.VK_DOWN:

                    if (tileDown == Contents.EMPTY || tileDown == Contents.GOAL) {
                        levelData[i][k] = Contents.EMPTY;
                        levelData[i][k-1] = Contents.PLAYER;
                    }

                    break;
            }

希望这会有所帮助。 :)