package main import ( "fmt" ) type Account struct { AccountNo string Pwd string Balance float64 } //存款 func (account *Account) Deposite() { var money float64 fmt.Print("请输入存款金额:") fmt.Scan(&money) if money <= 0 { fmt.Println("您输入的金额不正确") return } account.Balance += money fmt.Println("存款", money, "成功,当前金额为:", account.Balance) } //取款 func (account *Account) WithDraw() { var money float64 fmt.Print("请输入取款金额:") fmt.Scan(&money) if money <= 0 || money > account.Balance { fmt.Println("您输入的金额不正确") return } account.Balance -= money fmt.Println("取款", money, "成功,当前金额为:", account.Balance) } //查询 func (account *Account) query() { /** 存储 小数超 8 位时,会存在精度问题: (+1.99999999=3.6799999899999998,%f=3.680000) 存储 小数超 7 位时,不存在精度问题,但是会省略:(+1.9999999=3.6799999,%f=3.680000) 存储 小数超 6 位时,不存在精度问题,不会省略: (+1.999999=3.679999,%f=3.679999) */ fmt.Printf("你的账号为:%v 余额:%v (补充:%f)\n", account.AccountNo, account.Balance, account.Balance) } func main() { //测试 account := Account{ //AccountNo : "guangda123456", //Pwd : "111111", AccountNo: "1", Pwd: "1", Balance: 1.68, } var ano, pwd string fmt.Print("请输入账号:") fmt.Scanln(&ano) fmt.Print("请输入和密码:") fmt.Scanln(&pwd) if account.AccountNo == ano && account.Pwd == pwd { for { var number int fmt.Print("请选择您想要的操作(1.存款; 2.取款; 3.查询; 4.退出程序):") fmt.Scanln(&number) if number == 1 { account.Deposite() } if number == 2 { account.WithDraw() } if number == 3 { account.query() } if number == 4 { fmt.Println("已退出程序...") break } } } else { fmt.Println("用户名或者密码输入有误,请重新输入...") } }