简体   繁体   English

试图将 java if/else 转换为循环

[英]Trying to convert java if/else to loops

I'm building a Java game as part of my university studies and am trying to convert a large number of if/else statements into loops.我正在构建一个 Java 游戏作为我大学学习的一部分,并试图将大量 if/else 语句转换为循环。

The current, nonworking code is:当前的非工作代码是:

    public void kickBall() {
        if(ballDirection == 1) {
            for (int y = 0; y < 15; y++)
            {
                gamePosition[ballPositionX][ballPositionY].setIcon(bg_icon);
                gamePosition[ballPositionX][++ballPositionY].setIcon(ball_icon);
                if(y == baby2PositionY) {
                    gamePosition[ballPositionX][ballPositionY].setIcon(baby2_icon);
                    ballDirection = 2;
                }
            }

Eclipse console log: Eclipse 控制台日志:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 16
    at CBabyBallBounce.kickBall(CBabyBallBounce.java:613)
    at CBabyBallBounce$14.actionPerformed(CBabyBallBounce.java:516)
    at javax.swing.Timer.fireActionPerformed(Unknown Source)
    at javax.swing.Timer$DoPostEvent.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

line 613 indicated is the second gamePosition line, and 516 is the method call, but i'm having trouble understanding what the issue is and how I would rectify it?指示的第 613 行是第二个 gamePosition 行,第 516 行是方法调用,但我无法理解问题是什么以及如何纠正它?

previous code if it is useful..以前的代码如果有用的话..

        if (ballPositionY == 1 && ballDirection == 1) {
                gamePosition[ballPositionX][ballPositionY].setIcon(bg_icon);
                gamePosition[ballPositionX][++ballPositionY].setIcon(ball_icon);
            }
            else if(ballPositionY == 2 && ballDirection == 1) {
                gamePosition[ballPositionX][ballPositionY].setIcon(bg_icon);
                gamePosition[ballPositionX][++ballPositionY].setIcon(ball_icon);
            }
etc..

gamePosition code - this is within a 13x16 GridLayout JPanel: gamePosition 代码 - 这是在 13x16 GridLayout JPanel 内:

    private JButton gamePosition[][] = new JButton[13][16];
.....

    for (int i=0; i<13; i++)
        for (int j=0; j<16; j++)
        {
            JButton game_display = new JButton(bg_icon);
            game_display.setContentAreaFilled(false);
            game_display.setBorder(null);
            game_display.setFocusable(false);
            gamePosition[i][j] = game_display;
            gamePosition[i][j].setFocusable(false);

            if(j==8)
            {
                gamePosition[i][j].setIcon(wall_icon);
            }
        }

Thanks谢谢

There are two problems in your code:您的代码中有两个问题:

  1. You haven't checked the bounds of the arrays.您还没有检查 arrays 的边界。
  2. You haven't checked for null .您还没有检查null

Do it as follows:执行以下操作:

public void kickBall() {
    if (ballDirection == 1) {
        for (int y = 0; y < 15 && ballPositionX < gamePosition.length
                && ballPositionY < gamePosition[ballPositionX].length
                && gamePosition[ballPositionX][ballPositionY] != null; y++) {
            gamePosition[ballPositionX][ballPositionY].setIcon(bg_icon);
            ballPositionY++;
            if (ballPositionY < gamePosition[ballPositionX] && gamePosition[ballPositionX][ballPositionY] != null) {
                gamePosition[ballPositionX][ballPositionY].setIcon(ball_icon);
                if (y == baby2PositionY) {
                    gamePosition[ballPositionX][ballPositionY].setIcon(baby2_icon);
                    ballDirection = 2;
                }
            }
        }
        // ...
    }
    // ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM