编译错误,意外类型。必需:变量找到:值
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题
线路
grades.get(i) += currDiff;
将被处理为;
grades.get(i) =grades.get(i) + currDiff;
所以基本上总和的值将被分配给grades.get(i),这是不可能的。 (因为这是一个值本身,而不是一个变量)
解决方案
您可能需要将代码更改为:
当然,我的解决方案可能是错误的,具体取决于您到底想在这里做什么。
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;
Of course I could be wrong about the solution depending on what you exactly want to do here.
grades
是一个List
对象,get
方法返回一个值。你不能这样设置值!您可以执行以下操作:
grades
is aList
object andget
methods returns a value. You cannot set the value like that!Here is what you could do: