编译错误,意外的类型。要求:变量找到:值

Compilation error, unexpected type. required: variable found: value

public static List<Integer> gradingStudents(List<Integer> grades) {
    for(var i = 0; i < grades.size() - 1; i++){
        if(grades.get(i) >= 38){
            
            var currDiff = 5 - (grades.get(i) % 5);
            
            if(currDiff < 3){
                grades.get(i) += currDiff;
            }
        }
    }
    return grades;

}

我遇到编译错误,意外类型。必需:变量找到:值。有人可以在正确的方向轻推我吗?

问题
线 grades.get(i) += currDiff; 将被处理为; grades.get(i) = grades.get(i) + currDiff;

所以基本上总和的值将分配给 grades.get(i) 这是不可能的。 (因为这是一个值本身,而不是变量)

解决方案
您可能需要将代码更改为;

grades.set(i, grades.get(i) + currDiff);

当然,我的解决方案可能是错误的,具体取决于您在这里究竟想做什么。

grades 是一个 List 对象和 get 方法 returns 一个值。您不能那样设置值!

您可以执行以下操作:

public static List<Integer> gradingStudents(List<Integer> grades) {
    for(var i = 0; i < grades.size() - 1; i++){
        if(grades.get(i) >= 38){
            
            var currDiff = 5 - (grades.get(i) % 5);
            
            if(currDiff < 3){
                grades.set(i, grades.get() + currDiff);
            }
        }
    }
    return grades;

}