Go实现猜数字游戏

// GuessNumber project main.go
package main
 
  
import (
    "bufio"
    "fmt"
    "math/rand"
    "os"
    "strconv"
    "time"
)
 
  
func main() {
    fmt.Println("Guess Number Game is started. Please enjoy yourself")
    running := true
    reader := bufio.NewReader(os.Stdin)
    for running {
        fmt.Println("The number has been set. Please guess...")
        r := rand.New(rand.NewSource(time.Now().UnixNano()))
        theNumber := r.Intn(100)
        for running {
            fmt.Print("Your input number is:")
            data, _, _ := reader.ReadLine()
            command := string(data)
            if command == "stop" {
                running = false
            } else {
                inputNumber, err := strconv.Atoi(command)
                if err == nil {
                    if inputNumber > theNumber {
                        fmt.Println("Your guess number is greater than the number.")
                    } else if inputNumber < theNumber {
                        fmt.Println("Your guess number is lower than the number.")
                    } else {
                        fmt.Println("Congratulates! Your guess number is the number.\n")
                        break
                    }
                }
            }
        }
    }
}
 
  
 
 

你可能感兴趣的:(Go)