用这个Scanln函数怎么都不好使
fmt.Scanln(&operator)
接收字符型变量时只接收整型
并通过ASCII码值转换为字符型
改为用Scanf终于好使了,像c language一样
package main
import (
"fmt"
"unsafe"
)
func cal(n1 float64, n2 float64, operator byte) float64 {
var res float64
switch operator {
case '+':
res = n1 + n2
case '-':
res = n1 - n2
default:
fmt.Println("ERROR")
}
return res
}
func main() {
n1 := 1.2
n2 := 2.3
var operator byte
fmt.Println("plz input the operator")
fmt.Scanf("%c", &operator)
// test
fmt.Printf("Your input is %v, type is %T, it needs %d bytes\n", operator, operator, unsafe.Sizeof(operator))
ans := cal(n1, n2, operator)
fmt.Println("answer is", ans)
}