Java 连接四个边界外异常
这段代码非常粗糙、不完整,在任何方面都不专业。我收到: 线程“main”java.lang.ArrayIndexOutOfBoundsException中的异常:-1 位于ConnectFourGame.main(ConnectFourGame.java:63) 每当我尝试输入一个列时,就会显示该列。我一直在检查我的代码,我父亲也在检查,我们无法找到错误所在。任何帮助都会很好Java 连接四个边界外异常,java,exception,Java,Exception,这段代码非常粗糙、不完整,在任何方面都不专业。我收到: 线程“main”java.lang.ArrayIndexOutOfBoundsException中的异常:-1 位于ConnectFourGame.main(ConnectFourGame.java:63) 每当我尝试输入一个列时,就会显示该列。我一直在检查我的代码,我父亲也在检查,我们无法找到错误所在。任何帮助都会很好 import javax.swing.*; import java.awt.*; import java.awt.eve
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ConnectFourGame
{
public static void main(String[] args)
{
int[][] grid = new int[6][7];
int i, j, temp, player=0, inarowl=0, inarowr=0, temprow=0, inarowd=0, tempcol=0;
boolean legal = true;
boolean gameover = false;
//Initializing game board
for(i=0;i<6;i++)
for(j=0;j<7;j++)
grid[i][j] = 0;
//ALL GAME LOGIC
do {
i=0;
j=0;
//PLAYER 1
//check for diagonals next
do {
i=0;
j=0;
player=1;
//player places chip
temp=Integer.parseInt(JOptionPane.showInputDialog(null, "pick your column( 0 / 1 / 2 / 3 / 4 / 5 / 6 )"));
for(i=0;i<6;i++)
{
if(grid[i][temp]==0)
//detects lowest available chip slot in the column for placement
{ // and turns that spot into a box owned by the player
grid[i][temp]=player;
legal=true;
temprow=i;
tempcol=temp;
break;
}else legal=false;
}
}
while(!legal);
inarowl=0;
inarowr=0;
tempcol=temp;
do
{
tempcol--;
inarowl++;
}while(grid[temprow][tempcol]==player);
tempcol=temp;
do
{
tempcol++;
inarowr++;
}while(grid[temprow][tempcol]==player);
do
{
temprow--;
inarowd++;
}while(grid[temprow][tempcol]==player);
if (inarowl+inarowr-1>=4||inarowd>=4)
gameover=true;
//PLAYER 2
//check for diagonals next
do {
i=0;
j=0;
player=2;
temp=Integer.parseInt(JOptionPane.showInputDialog(null, "pick your column( 0 / 1 / 2 / 3 / 4 / 5 / 6 )"));
for(i=0;i<6;i++)
{
if(grid[i][temp]==0)
{
grid[i][temp]=player;
legal=true;
temprow=i;
tempcol=temp;
break;
}else legal=false;
}
}
while(!legal);
inarowl=0;
inarowr=0;
tempcol=temp;
do
{
tempcol--;
inarowl++;
}while(grid[temprow][tempcol]==player);
tempcol=temp;
do
{
tempcol++;
inarowr++;
}while(grid[temprow][tempcol]==player);
do
{
temprow--;
inarowd++;
}while(grid[temprow][tempcol]==player);
if (inarowl+inarowr-1>=4||inarowd>=4)
gameover=true;
} while(gameover==false);
}
}
import javax.swing.*;
导入java.awt.*;
导入java.awt.event.*;
公共类游戏
{
公共静态void main(字符串[]args)
{
int[][]网格=新int[6][7];
inti,j,temp,player=0,inarowl=0,inarowr=0,temprow=0,inarowd=0,tempcol=0;
布尔合法=真;
布尔gameover=false;
//初始化游戏板
对于(i=0;i=4)
gameover=true;
}while(gameover==false);
}
}
这段代码可能会触发一个越界异常:如果grid[temprow][0]==player,则tempcol减小(为-1),grid[temprow][1]将被计算
do
{
tempcol--;
inarowl++;
}while(grid[temprow][tempcol]==player);
在while条件下,还必须确保数组索引仍在数组的边界内
while (grid[temprow][tempcol] == player && tempcol > 0 && tempcol < grid[temprow].length)
while(grid[temprow][tempcol]==player&&tempcol>0&&tempcol
我看到的主要问题是您使用的是do。。。while(条件)
循环。本例中的问题是,首先要减去整数,然后使用相同的整数访问内存位置
这种方法的问题在于,由于在进行减法之前要进行检查,因此试图访问负内存位置。要解决此问题,请改用
while(condition)
loop。这将允许您在执行阵列操作之前执行检查,这将允许您及时中断循环。A我明白您的意思。我想,通过从inarowl+inarowr中减去1,它仍然会计算相同的数量。我要试试这个@user3914322:我在回答中遗漏了一点。您可能还需要确保值永远不会低于0。