cannot use i % 3 == 0 (type untyped bool) as type int

cannot use i % 3 == 0 (type untyped bool) as type int
强烈推介IDEA2021.1.3破解激活,IntelliJ IDEA 注册码,2021.1.3IDEA 激活码  

大家好,我是架构君,一个会写代码吟诗的架构师。今天说一说cannot use i % 3 == 0 (type untyped bool) as type int,希望能够帮助大家进步!!!

报错代码

package main

import "fmt"

func main() {
   
	g(15)
}

func g(i int) {
   
	switch i{
   
	case i%3==0:
		fmt.Print("Fizz")
	case i%5==0:
		fmt.Print("Buzz")
	case i%3==0 && i%5==0:
		fmt.Print("FizzBuzz")
	}
}

报错信息

./main.go:12:2: cannot use i % 3 == 0 (type untyped bool) as type int
./main.go:14:2: cannot use i % 5 == 0 (type untyped bool) as type int
./main.go:16:2: cannot use i % 3 == 0 && i % 5 == 0 (type bool) as type int

报错原因switch使用方式错误,修改方法,把i改为true或去掉i

package main

import "fmt"

func main() {
   
	g(15)
}

func g(i int) {
   
	switch {
   
	case i%3==0:
		fmt.Print("Fizz")
	case i%5==0:
		fmt.Print("Buzz")
	case i%3==0 && i%5==0:
		fmt.Print("FizzBuzz")
	}
}
本文来源it小白求知路,由架构君转载发布,观点不代表Java架构师必看的立场,转载请标明来源出处:https://javajgs.com/archives/29151

发表评论