Go实现猜数字游戏

xiaoxiao2021-02-28  4

// 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                     }                 }             }         }     } }
转载请注明原文地址: https://www.6miu.com/read-2400328.html

最新回复(0)