尝试将Java的if/else转换为循环。

huangapple go评论80阅读模式
英文:

Trying to convert java if/else to loops

问题

我正在进行大学学习中的Java游戏开发,试图将大量的if/else语句转换为循环。

目前的代码有问题,如下所示:

	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控制台日志:

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)

第 613 行标示第二个 gamePosition 行,第 516 行标示方法调用,但我很难理解问题所在,以及如何解决它?

之前的代码,如果有用的话..

		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);
			}
等等..

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);
			}
		}

谢谢

英文:

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.

The current, nonworking code is:

	public void kickBall() {
		if(ballDirection == 1) {
			for (int y = 0; y &lt; 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:

Exception in thread &quot;AWT-EventQueue-0&quot; 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?

previous code if it is useful..

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

gamePosition code - this is within a 13x16 GridLayout JPanel:

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

	for (int i=0; i&lt;13; i++)
		for (int j=0; j&lt;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

答案1

得分: 1

在你的代码中有两个问题:

  1. 你没有检查数组的边界。
  2. 你没有检查是否为 null

请按以下方式修改:

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].length && gamePosition[ballPositionX][ballPositionY] != null) {
                gamePosition[ballPositionX][ballPositionY].setIcon(ball_icon);
                if (y == baby2PositionY) {
                    gamePosition[ballPositionX][ballPositionY].setIcon(baby2_icon);
                    ballDirection = 2;
                }
            }
        }
        // ...
    }
    // ...
}
英文:

There are two problems in your code:

  1. You haven't checked the bounds of the arrays.
  2. You haven't checked for null.

Do it as follows:

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

huangapple
  • 本文由 发表于 2020年4月9日 23:28:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/61124668.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定