gpt4 book ai didi

java - 检查二维数组列是否已满

转载 作者:行者123 更新时间:2023-12-02 04:38:42 25 4
gpt4 key购买 nike

我正在开发一个游戏类项目,但遇到了困难。

我一直在寻找如何检查二维数组中的任何列是否已满,如果是,则该列被完全清除。

我对Java非常陌生,所以如果你可以的话请帮我解决这个问题!

这是我到目前为止的代码。

//removes filled columns - added method
private boolean removeFullCol()
{
for(int i = 0; i < board.length; i++){
for(int j = 0; j < board[i].length; j++){
if(board[i][j] != occupied){
return false;
}
}
}
//empty square was never found - column is full
return true;
}

最佳答案

如果您只想删除完整的列,请按列进行检查,但是如果您想检查所有列,则无法返回 boolean 值。您有 2 个选择:

  • 检查给定列(返回 boolean 值)
  • 返回第一列已满(返回 int)

第一个选项:

private boolean removeFullCol(int col)
for (int row=0; row<board[col].length; row++) {
// check if all are filled not sure which object is inside board...
if(board[row][col] == null){
return false;
}
}
return true;
}

第二个选项(返回第一列完整或数据,否则返回 -1):

private int removeFullCol()
for (int row=0; row<board[col].length; row++) {
int filled = 0;
for (int row=0; row<board[col].length; row++) {
// check if all are filled not sure which object is inside board...
if(board[row][col] != null){
filled ++;
}
}

// when finished check how many rows are filled
if (board[col].length == filled)
return row;

}
return -1;
}

即时编写了代码...如果有任何错误或疑问,请告诉我...

关于java - 检查二维数组列是否已满,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30440507/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com