Java 必需:找到的变量:值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16482488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:07:12  来源:igfitidea点击:

Required: Variable Found: Value

javaif-statementsyntax-error

提问by Alex

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?

出于某种原因,if 语句中的第二行以意外类型返回 - 必需的:找到的变量:值。我试过 == 显然没有用。任何见解?

采纳答案by Sirko

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.

Math.abs(a[i]-a[i-1])只返回一个 int 值(没有变量引用或类似的)。所以你试图为一个值分配一个新值。这是不可能的。您可以只为变量分配一个新值。

回答by Keppil

You have reversed your assign statement. Change it to

你已经撤销了你的赋值语句。将其更改为

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

回答by Theodoros Chatzigiannakis

You are trying to assign the value of biggestGapto 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.

您正在尝试将 的值分配给biggestGap返回的数字Math.abs()。当然,您不能,因为该值取决于Math.abs()包含的内容以及它如何处理其参数。

Perhaps you meant the opposite:

也许你的意思相反:

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