存在一个问题,提示用户输入票价年龄,但当if
输入的年龄高于 13 岁时,报表仅打印 9.99:
尝试过:=
,并且fmt.Println(ticketPrice == 19.99)
。
如果有人有其他建议,请告诉我。
package main
import "fmt"
func main() {
var age int
var ticketprice float64
defer fmt.Println("Your age is:", getAge(age)) // calls getAge
defer fmt.Println("Your ticket price is:", printTicket(age,
ticketprice)) // calls printTicket
}
func printTicket(age int, ticketPrice float64) float64 {
if age <= 13 {
ticketPrice = 9.99 // only calls 9.99, not anything else
} else if age > 13 && age < 65 {
ticketPrice = 19.99 // not being called?
} else if age >= 65 {
ticketPrice = 12.99 // not being called?
}
return ticketPrice // returns ticket price
} // Whole function is not being used for some reason. Only returns 9.99, nothing else
func getAge(age int) int {
fmt.Println("What is your age?")
fmt.Scan(&age)
for age < 0 || age > 100 {
fmt.Println("That cannot be, please enter your age again")
fmt.Scan(&age)
}
return age
}
func getName(name string) string {
fmt.Println("What is your name?")
fmt.Scan(&name)
return name
}
尚方宝剑之说
相关分类