Required: Variable Found: Value

扶醉桌前 提交于 2019-12-08 01:26:38

问题


public static int biggestArrayGap(int []a, int n)
{
int biggestGap = Math.abs(a[1]-a[0]);
    for (int i=1; i<n-1; i++)
{
    if (Math.abs(a[i]-a[i-1]) > biggestGap)    
        Math.abs(a[i]-a[i-1]) = biggestGap;
}
    return biggestGap;
}

For some reason, the second line in the if statement is returning as unexpected type– required: variable found: value. I tried == and that obviously didn't work. Any insight?


回答1:


You switched the operands in your assign statement.

Switch this

Math.abs(a[i]-a[i-1]) = biggestGap;

to this

biggestGap = Math.abs(a[i]-a[i-1]);

Math.abs(a[i]-a[i-1]) returns just an int value (no variable reference or similar). So your trying to assign a new value to a value. Which is not possible. You can just assign a new value to a variable.




回答2:


You have reversed your assign statement. Change it to

biggestGap = Math.abs(a[i]-a[i-1]);



回答3:


You are trying to assign the value of biggestGap to the number returned by Math.abs(). Naturally, you can't, because that value depends on what Math.abs() contains and how it handles its arguments.

Perhaps you meant the opposite:

biggestGap = Math.abs(a[i]-a[i-1]);


来源:https://stackoverflow.com/questions/16482488/required-variable-found-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!