ZhangZhihui's Blog  

 

package main

import "fmt"

func main() {
    fmt.Println("Welcome to Go Bank!")
    fmt.Println("What do you want to do?")
    fmt.Println("1. Check balance")
    fmt.Println("2. Deposit money")
    fmt.Println("3. Withdraw money")
    fmt.Println("4. Exit")

    var choice int
    fmt.Scan(&choice)

    fmt.Println("Your choice:", choice)
}

 

1

 

 

package main

import (
    "fmt"
    "os"
)

func main() {
    var investmentAmount float64

    fmt.Print("Investment Amount: ")
    _, err := fmt.Scanln(&investmentAmount)
    if err != nil {
        fmt.Println("Invalid input. Please input a number then press enter.")
        os.Exit(1)
    }
}

 

1

 

package main

import (
    "bufio"
    "fmt"
    "os"
    "strings"
)

func main() {
    fmt.Println("Welcome to Go Bank!")
    fmt.Println("What do you want to do?")
    fmt.Println("1. Check balance")
    fmt.Println("2. Deposit money")
    fmt.Println("3. Withdraw money")
    fmt.Println("4. Exit")

    reader := bufio.NewReader(os.Stdin)

    text, err := reader.ReadString('\n')
    if err != nil {
        fmt.Println(err)
        return
    }

    text = strings.TrimSuffix(text, "\n")
    text = strings.TrimSuffix(text, "\r")

    fmt.Printf("Your choice: %v\n", text)
}

 

1

 

posted on 2026-03-03 20:55  ZhangZhihuiAAA  阅读(17)  评论(0)    收藏  举报