Java 创建两个方法,返回布尔方法?
我只是想知道我是否可以在我的程序中得到一些帮助,这些需要: 现在添加两个公共方法来获取和设置此新数组的值: public void uncover(int thisCol,int thisRow)uncover方法将指定正方形的状态更改为false。否则,如果输入坐标在雷区之外,或者方格已经被发现,那么它什么也不做 public boolean isCovered(int thisCol,int thisRow)如果覆盖了指定的正方形,isCoveredmethod将返回true。否则,如果输入坐标在雷区外或方格未被覆盖,则返回false 我尝试过创建下面的这些方法,但我认为它们不正确,请大家看一下好吗Java 创建两个方法,返回布尔方法?,java,Java,我只是想知道我是否可以在我的程序中得到一些帮助,这些需要: 现在添加两个公共方法来获取和设置此新数组的值: public void uncover(int thisCol,int thisRow)uncover方法将指定正方形的状态更改为false。否则,如果输入坐标在雷区之外,或者方格已经被发现,那么它什么也不做 public boolean isCovered(int thisCol,int thisRow)如果覆盖了指定的正方形,isCoveredmethod将返回true。否则,如果输入
public void uncover(int thisCol, int thisRow) {
if(thisCol <0 || thisRow < 0)
return null;
if(thisCol>=numCols || thisRow>=numRows)
return null;
}
public boolean isCovered(int thisCol, int thisRow){
if(thisCol >0 || thisRow > 0)
return true;
if(thisCol>=numCols || thisRow>=numRows)
return true;
else;
return null;
}
public void uncover(int thisCol,int thisRow){
如果(thisCol=numCols | | thisRow>=numRows)
返回null;
}
已发现公共布尔值(int thisCol,int thisRow){
如果(thisCol>0 | | thisRow>0)
返回true;
如果(thisCol>=numCols | | thisRow>=numRows)
返回true;
其他的
返回null;
}
第一种方法:-
public void discover(intthiscol,intthisrow)
这是一种无效的方法。这意味着您不能返回任何值(null、true或false)
第二种方法:
public boolean被覆盖(int thisCol,int thisRow)
不能返回null,因为返回类型为布尔值。所以应该是返回false代码>
以上更改需要更正。之后,您可以尝试使用您的代码。我的理解(在C#)是,“public void”表示您没有向调用者返回任何内容。所以在“uncover”方法中,我希望它在尝试返回Null时会给您一个错误
另外,在第二个示例中,我希望在“return null”行上也会看到一个错误,因为您的返回类型是布尔型的。假设数组是在变量的类中声明的:
private boolean thisArray[][];
以下是正确的uncover
功能:
public void uncover(int thisCol, int thisRow) {
if(thisCol < 0 || thisRow < 0) return;
if(thisCol >= numCols || thisRow >= numRows) return;
thisArray[thisCol][thisRow] = true;
}
public boolean isCovered(int thisCol, int thisRow){
if(thisCol < 0) return false;
if(thisRow < 0) return false;
if(thisCol >= numCols) return false;
if(thisRow >= numRows) return false;
return thisArray[thisCol][thisRow];
}
更正:
如果坐标在正方形外,则必须返回false
,即返回true
李>
您没有检查我在最后一行添加的数组的有效值:returnthisarray[thisCol][thisRow]代码>
在布尔值中,不返回false。您正在返回null。布尔值不能为null。如果返回类型为void,则不能返回任何内容。您只能返回。不能无效返回null您实际上没有在任何地方使用此雷区数组。to CathalMF:布尔值可以为null,但布尔值不能为null