java 预期变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36165743/
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
Variable expected
提问by Andrew Tobilko
private void calculateForEachQuestion() {
...
if (questions.size() >= answers.size()) {
answers.forEach(answer -> {
if (questions.contains(answer))
this.answer.setPoints((this.answer.getPoints())++);
// Variable expected ^
});
}
The error I encountered is:
我遇到的错误是:
unexpected type
required: variable
found: value
需要意外类型:
找到的变量:值
What is wrong with the statement?
声明有什么问题?
this.answer.setPoints((this.answer.getPoints())++);
回答by qwwqwwq
The ++
operator only makes sense when applied to a declared variable.
在++
当应用于声明的变量操作才有意义。
This operator will add one to an integer
, long
, etc. and save the result to the same variable in-place.
此运算符会将 1 添加到 an integer
、long
等,并将结果就地保存到相同的变量中。
If there is no declared variable, there is nowhere to save the result, which is why the compiler complains.
如果没有声明变量,则结果无处保存,这就是编译器抱怨的原因。
One way to allow use of the ++
operator in this situation would be (not the most concise code, but to illustrate the point):
++
在这种情况下允许使用运算符的一种方法是(不是最简洁的代码,而是为了说明这一点):
int myVariable = this.answer.getPoints();
myVariable++;
this.answer.setPoints(myVariable);
回答by user3437460
this.answer.getPoints()
will return a value, and you can't use increment ++
or decrement --
on that. You can only use it on variables.
this.answer.getPoints()
将返回一个值,您不能对其使用增量++
或减量--
。您只能在变量上使用它。
If you want to add 1 to it, you can do it as:
如果你想给它加 1,你可以这样做:
this.answer.setPoints((this.answer.getPoints())+1);
I know that, but why?
我知道,但为什么呢?
You can do this:
你可以这样做:
int x = 5;
x++; // x is a variable and can have value 6
but not this:
但不是这个:
5++; // 5 is a value and can't have value 6
回答by Atri
Change this:
改变这个:
this.answer.setPoints((this.answer.getPoints())++);
to:
到:
this.answer.setPoints((this.answer.getPoints())+1);
++
will increment a variable with 1, but since this.answer.getPoints()
will return a value and its not a defined variable, it won't be able to store the incremented value.
++
将用 1 增加一个变量,但由于this.answer.getPoints()
将返回一个值而不是一个定义的变量,它将无法存储增加的值。
Think of it like doing:
this.answer.getPoints() = this.answer.getPoints() + 1
, where would the incremented value be stored?
把它想象成:
this.answer.getPoints() = this.answer.getPoints() + 1
,增加的值将存储在哪里?
回答by Paul Hicks
The error is just in this bit:
错误就在这一点上:
(this.answer.getPoints())++
The first part of this (this.answer.getPoints())
creates an rvalue: effectively, an anonymous variable that lasts almost no time.
第一部分(this.answer.getPoints())
创建了一个右值:实际上,一个几乎没有时间持续的匿名变量。
The second part ++
increments that anonymous variable afterit is passed into setPoints()
.
第二部分++
增量匿名变量后,它被传递到setPoints()
。
Then, the anonymous variable (now incremented) is thrown away.
然后,匿名变量(现在递增)被丢弃。
The compiler/IDE knows that you're doing something that will never be used (incrementing a temporary value), and is flagging it.
编译器/IDE 知道您正在做一些永远不会被使用的事情(增加一个临时值),并且正在标记它。