如果您检查的网格中没有任何内容,如何返回零?

时间:2016-05-26 04:14:59

标签: java loops for-loop while-loop

开始时,我正在制作游戏。你在一个3x3网格上战斗(使用一个二维数组),如果" Lane#" (Lane#= Row + Col)在你前面是空白然后你得到了-15%的伤害减免,并且每个空白道都会叠加。

这意味着如果你在[0] [0],那么你在第0巷,因此,不可能有任何人在你之前,你将永远采取100%的伤害(这当然没有防御和修改的yadda yadda其他)

如果你在[2] [2],那么你在第4巷,如果你前面的每个车道至少有一个空间,那么你将获得15 * 4 = 60,100-60 =实际伤害的40%。

现在已经不在了。我很难返回0 ...我一直收到错误,说你无法返回Void值...

'无法从结果类型为void'

的方法返回值
public Blanks(int l) { //l = Lane
    int x = 0; //The Return
    for (int i = 0; i < 6; i++) //The loop
        if (l=0){ //Here I keep getting an error saying 'incompatible types'
            x = 0;
            return x; //Here is the 'cannot return a void value' error
            break;
        }
        if (l>=1){
            x++;
        }
        if (l>=2){
            x++;
        }
        if (l>=3){
            x++;
        }
        if (l>=4){
            x++;
        }
        return x; //for some odd reason, this is also a void value
    }
}

我还没有添加检查数组/网格部分,因为我也难以理解那个...但是另一个问题,另一个问题......实际数组本身..

3 个答案:

答案 0 :(得分:0)

要返回整数值,您必须在方法中提及返回类型。此外,在第一个if语句中,您使用了赋值运算符而不是比较。

也是为什么你在回归后使用了休息。我认为你必须先做假,然后最后回来。

还有一件事要补充。你的for循环应该包含大括号。只会根据您的代码执行第一个if语句。

public int Blanks(int l) { //l = Lane
    int x = 0; //The Return
    for (int i = 0; i < 6; i++) //The loop
        if (l==0){ //Here I keep getting an error saying 'incompatible types'
            x = 0;
            break;
        }
        if (l>=1){
            x++;
        }
        if (l>=2){
            x++;
        }
        if (l>=3){
            x++;
        }
        if (l>=4){
            x++;
        }
        return x; //for some odd reason, this is also a void value
    }
}

我还没有进入你的逻辑。如果您在此之后遇到任何问题,请评论。

答案 1 :(得分:0)

您应该将方法标题修改为public int Blanks(int l) {
并且您应该删除break;关键字,因为您在它之前返回方法值并且将是未到达的语句。

答案 2 :(得分:0)

我不明白为什么你在这里使用for循环,但这是一种方法:

public int Blanks(int l) { 
    int x = 0;
    for (int i = 0; i < 6; i++)
        if (l==0){
            x = 0;
        }else {
            x++;
        }
    return x; 
}

但是如果l==0您的方法将返回5;

如果您想返回01,则需要删除for循环

public int Blanks(int l) {
    if (l==0) return 0;
    else return 1;
}

使用true-false的方法:

public boolean Blanks(int l) {
    if (l==0) return false;
    else return true;
}