[英]Variable declared and not used in a loop
Got confusion with a function 对功能感到困惑
package main
import "fmt"
func dominantIndex(nums []int) int {
var max, max2 = -12423421, -12423421
var i, j = -1, -1
for k, num := range nums {
if num > max {
max, max2 = num, max
i, j = k, i
} else if num > max2 {
max2 = num
j = k
}
}
if max >= max2*2 {
return i
}
return -1
}
func main() {
var a = []int{3, 6, 100, 1, 0 }
fmt.Print(dominantIndex(a))
}
I have to insert a nonsense statement in the loop such as j = j
. 我必须在循环中插入一个无意义的语句,例如
j = j
。 Otherwise, it raises ./hello.go:7:6: j declared and not used
. 否则,它将引发
./hello.go:7:6: j declared and not used
。 Wonder if there is any fix. 想知道是否有任何修复。
You assign a value to j, but you don't use j. 您为j分配了一个值,但未使用j。 That is the problem.
那就是问题所在。 You could as well leave j out, without changing the functionality of the code.
您也可以不使用j而无需更改代码的功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.