Java 如何处理点击而不让其超出范围 public void processLightClick(int行,int列){ 州[行][col]=!州[行][col]; 州[行+1][col]=!州[行+1][col]; 州[row-1][col]=!州[row-1][col]; 州[行][col+1]=!州[行][col+1]; 州[行][col-1]=!州[行][col-1]; 对于(inti=-1;i
您无法避免它,请检查Java 如何处理点击而不让其超出范围 public void processLightClick(int行,int列){ 州[行][col]=!州[行][col]; 州[行+1][col]=!州[行+1][col]; 州[row-1][col]=!州[row-1][col]; 州[行][col+1]=!州[行][col+1]; 州[行][col-1]=!州[行][col-1]; 对于(inti=-1;i,java,Java,您无法避免它,请检查 public void processLightClick(int row, int col) { //loop for the inner square for (row = 1; row < GAME_SIZE - 1; row++){ states[row][col] = !states[row][col]; states[row+1][col] = !states[row+1][col];
public void processLightClick(int row, int col) {
//loop for the inner square
for (row = 1; row < GAME_SIZE - 1; row++){
states[row][col] = !states[row][col];
states[row+1][col] = !states[row+1][col];
states[row-1][col] = !states[row-1][col];
states[row][col+1] = !states[row][col+1];
states[row][col-1] = !states[row][col-1];
}
//loop for the outer sections
for(row = 0; row < GAME_SIZE; row++){
states[row][col] = !states[row][col];
states[row-1][col] = !states[row-1][col];
if(states[row][col] = states[0][0]){
states[row][col+1] = !states[row][col+1];
}
if(states[row][col] = states[0][4]){
states[row][col-1] = !states[row][col-1];
}
}
changeLight();
turns++;
System.out.println(row+":"+col);
status.setText("Turn Number " + turns);
}
enter code here
行+i=0
第j行>=0
不要使用for循环,将4种情况一一写入。检查上述约束您可以检查是否单击了其中一个边框单元格,然后调用appropiate方法更改其状态和颜色,如
row+i < max
col+j < max
row-i >= 0
row-j >=0
列也是如此,最后更改单击的单元格
您可以通过创建一个方法来实现这一点,该方法接收用于更改单元格(i,j)的参数i
和j
,并且您只需在每种情况下使用正确的参数
另外,我假设states
是一个布尔2d数组,因此不需要检查==true
,您可以在另一种情况下使用!states[I][j]
(这是不必要的,如果它不为true,则必须为false)
编辑:
像这样的
if(row == 0) // it is the first row
// call the method for changing the cell underneath
else if(row == n-1) // assuming n is the number of cells
// call the method for changing the cell above
else
// call both methods
和您的
changeLight(行、列)
应更改指定单元格的状态和颜色否,不要按照@DavidZhou的建议执行,因为此错误不应是捕获的异常。相反,您需要做的是在访问数组元素之前检查边界条件,而不是尝试从越界元素获取数组元素。这只是基本的算术和肘部润滑。实际上,你应该能够在一张纸上弄清楚这一点。捕捉异常被认为是糟糕的做法。相反,检查单击的方块是否是边框方块,然后采取适当的操作比捕捉和挤压异常更优雅。如果你有一些你的代码还有什么其他错误导致了你不希望发生的异常,并且由于异常被挤压,没有收到任何关于的反馈?(@HovercraftFullOfEels:在我单击“comment”:D之前几秒钟就从我嘴里说出了这些话)抱歉,伙计们,例外部分并不是我真正的意思。我删除了评论,取而代之的是,做了一个(希望如此)对他来说,这是一个有用的解决方案。我不确定我是否理解你说的话。我将for循环移出并引入了一个新方法,使其编码更加简单,但我的问题在于processLightClick中的前5行。我尝试创建一个从1开始的for循环(而不是0)然后运行,直到我想说的是,你不需要一个循环通过你所有的细胞,你可以单独检查它们(你只需要检查4个)
if(row == 0) // it is the first row
// call the method for changing the cell underneath
else if(row == n-1) // assuming n is the number of cells
// call the method for changing the cell above
else
// call both methods
public void processLightClick(int row, int col) {
{
if(row == 0) // it is the first row, change the one below only
changeLight(row+1,col);
else if(row == n-1)
changeLight(row-1,col); //change the one above only
else
{ //change both
changeLight(row+1,col);
changeLight(row-1,col);
}
if(col == 0)
changeLight(row,col+1);
else if(col == n-1)
changeLight(row,col-1);
else
{
changeLight(row,col+1);
changeLight(row,col-1);
}
changeLight(row,col); // the clicked cell
}