英文:
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 < 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 "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?
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:
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
答案1
得分: 1
在你的代码中有两个问题:
- 你没有检查数组的边界。
- 你没有检查是否为
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:
- You haven't checked the bounds of the arrays.
- You haven't checked for
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;
}
}
}
// ...
}
// ...
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论