我的div(4,2)程序应返回(0,true),而div(4,3)应返回(1,false)。
package main
import "fmt"
func div(a int, b int) (int, bool) {
if a%b == 0 {
c := a % b
return c, true
} else {
c := a % b
return c, true
}
}
func main() {
fmt.Println(div(4, 2)) // should return (0, true)
fmt.Println(div(4, 3)) // should return (1, false)
}
游乐场:https://play.golang.org/p/NyiGmd03SGE
输出:
0 true
1 true
答案 0 :(得分:2)
例如,
package main
import "fmt"
func div(a int, b int) (int, bool) {
if a%b == 0 {
c := a % b
return c, true
} else {
c := a % b
return c, false
}
}
func main() {
fmt.Println(div(4, 2)) // should return (0, true)
fmt.Println(div(4, 3)) // should return (1, false)
}
游乐场:https://play.golang.org/p/pEh55lBUFJI
输出:
0 true
1 false
或者简单地
package main
import "fmt"
func div(a int, b int) (int, bool) {
c := a % b
return c, c == 0
}
func main() {
fmt.Println(div(4, 2)) // should return (0, true)
fmt.Println(div(4, 3)) // should return (1, false)
}
游乐场:https://play.golang.org/p/zIZvFMdzZqn
输出:
0 true
1 false