预期变量 [英] Variable expected

查看:234
本文介绍了预期变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void calculateForEachQuestion() {
    ...
    if (questions.size() >= answers.size()) {
        answers.forEach(answer -> {
            if (questions.contains(answer))
                this.answer.setPoints((this.answer.getPoints())++);
                // Variable expected  ^                    
    });
}

我遇到的错误是:

意外类型
必需:变量
找到:值

unexpected type
required: variable
found: value

该语句有什么问题?

this.answer.setPoints((this.answer.getPoints())++);

推荐答案

++运算符仅在应用于声明的变量时才有意义.

The ++ operator only makes sense when applied to a declared variable.

此运算符将一个加到integerlong等上,并将结果保存到原位相同的变量中.

This operator will add one to an integer, long, etc. and save the result to the same variable in-place.

如果没有声明的变量,则无处保存结果,这就是编译器抱怨的原因.

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

这篇关于预期变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆