我对此很陌生。我正在尝试制作一个接收两个类型数字的计算器float64,但随后我想输入两位小数的结果。我一直在读它是与"%.2f"但它的结果不太好fmt.Scanf
package main
import "fmt"
const menu string = "Calculator v1.0:\n\t1.a+b\n\t2.a-b\n\t3.a*b\n\t4.a/b\nSelect an option:"
func main() {
var option int
var firstNumber float64
var secondNumber float64
fmt.Println(menu)
fmt.Scanf("%d", &option)
fmt.Println("First number:")
fmt.Scanf("%.2f", &firstNumber)
fmt.Println("Second number:")
fmt.Scanf("%.2f", &secondNumber)
if option == 1 {
println(sum(firstNumber, secondNumber))
} else if option == 2 {
println(subtract(firstNumber, secondNumber))
} else if option == 3 {
println(multiply(firstNumber, secondNumber))
} else if option == 4 {
println(divide(firstNumber, secondNumber))
}
}
func sum(a float64, b float64) float64 {
return a + b
}
func subtract(a float64, b float64) float64 {
return a - b
}
func multiply(a float64, b float64) float64 {
return a * b
}
func divide(a float64, b float64) float64 {
return a / b
}
牛魔王的故事
相关分类