编译错误,意外类型。必需:变量找到:值

发布于 2025-01-16 19:40:44 字数 444 浏览 3 评论 0原文

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;

}

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

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;

}

I'm getting a Compilation error, unexpected type. required: variable found: value. Can someone nudge me in the right direction please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吃素的狼 2025-01-23 19:40:45

问题

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

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

解决方案

您可能需要将代码更改为:

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

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

Problem

The line
grades.get(i) += currDiff;
Will be processed as;
grades.get(i) = grades.get(i) + currDiff;

So basically the value of the sum will be assigned to the grades.get(i) which is not possible. ( As this is a value itself, not a variable)

Solution

What you probably need to change the code as;

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

Of course I could be wrong about the solution depending on what you exactly want to do here.

給妳壹絲溫柔 2025-01-23 19:40:45

grades 是一个 List 对象,get 方法返回一个值。你不能这样设置值!

您可以执行以下操作:

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;

}

grades is a List object and get methods returns a value. You cannot set the value like that!

Here is what you could do:

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;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文