go language-use-examples-select random numbers

Guess number game

  1. Generate random integer (0, 100)
  2. Prompt the user to enter the guessed number in the console
  3. In comparison, when the user input is large, the prompt is too large
  4. When the user input is too small, the prompt is too small
  5. When the user input is correct, the prompt is correct after N times, too smart
  6. The user guesses up to 5 times. If the guess is not correct within 5 times, the prompt is too stupid and the game is over
  7. After success or failure, prompt the user whether to continue, enter: yes, y, Y to continue, regenerate random numbers, let the user guess

 

package main

import (
    "fmt"
    "math/rand"
    "time"
)

func main() {
    for {
        var num int
        rand.Seed(time.Now().Unix())
        randNum := rand.Int() % 100
        fmt.Println(randNum)
        var isOK bool
        for i := 1; i <= 5; i++ {
            fmt.Print("请输入你猜的数字:")
            fmt.Scan(&num)
            ifrandNum> num { 
                fmt.Printf ( " The number you guessed is too small, you still have% d chances, please re-enter: \ n " , 5 - i) 
            } else  if randNum < num { 
                fmt.Printf ( " You guess The number is too large, you still have% d chances, please re-enter: \ n " , 5 - i) 
            } else { 
                fmt.Println ( " The number you guessed is completely correct! " ) 
                IsOK = true 
                break 
            } 
        } 
        if isOK ! = true {
            fmt.Println ( " 5 chances to run out, exit " ) 
        } 
        var txt string 
        fmt.Print ( " Will you continue to guess the number: (y / n) " ) 
        fmt.Scan ( & txt)
         if txt! = " y " { 
            fmt.Println ( " Exit " )
             break 
        } 
    } 

}

 

Guess you like

Origin www.cnblogs.com/malukang/p/12723150.html