-6

我只是想知道是否可以对我的程序有一些帮助,这些需要:

现在添加两个公共方法来获取和设置这个新数组的值: public void discover(int thisCol, int thisRow) unlock 方法将指定方格的状态更改为 false。否则,如果输入坐标在雷区之外,或者方格已经被发现,它什么也不做。

public boolean isCovered(int thisCol, int thisRow)如果指定的方格被覆盖,isCovered方法返回true。否则,如果输入坐标在雷区之外或方格未被覆盖,则返回 false。

我尝试在下面创建这些方法,但我认为它们不正确,请任何人看一看?

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;
}
4

3 回答 3

0

我的理解(在 C# 中)是“public void”表示您没有向调用者返回任何内容。所以在“发现”方法中,我希望它在尝试返回 Null 时给你一个错误。

另外,在第二个中,我希望在“return null”行上也看到一个错误,因为您的返回类型是布尔值。

于 2013-03-12T15:18:25.190 回答
0

第一种方法:-

public void uncover(int thisCol, int thisRow)

这是一种无效的方法。这意味着你不能返回任何值(null, true or false)

第二种方法:-

public boolean isCovered(int thisCol, int thisRow)

你不能返回 null 因为返回类型是布尔值。所以应该是return false;

以上更改需要更正。之后您可以尝试使用您的代码。

于 2013-03-12T15:16:42.647 回答
0

假设数组在类中的变量中声明:

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;
}

更正:

  1. 没有值的 return 语句,因为函数返回void
  2. 最后一行数组值的赋值:thisArray[thisCol][thisRow] = true

这是正确的isCovered功能:

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];
}

更正:

  1. 如果坐标在它必须返回的正方形之外false,那么你正在返回true
  2. 您缺少检查我在最后一行添加的数组的有效值:return thisArray[thisCol][thisRow];
于 2016-04-23T03:55:58.733 回答