v1,使用随机数案例
package main import ( "fmt" "math/rand" ) func main() { maxNum := 100 secretNumber := rand.Intn(maxNum) fmt.Println("The secret number is ", secretNumber) } --------------------------------- 发现总是输出一个数字:原因,没有设计随机种子
v2,加入随机种子
package main import ( "fmt" "math/rand" "time" ) func main() { maxNum := 100 // 设置随机种子 rand.Seed(time.Now().UnixNano()) secretNumber := rand.Intn(maxNum) fmt.Println("The secret number is ", secretNumber) } -------------------------------- 现在就可以做出伪随机的效果啦
v3,搭出代码框架
package main import ( "bufio" "fmt" "math/rand" "os" "strconv" "strings" "time" ) func main() { maxNum := 100 // 随机种子 rand.Seed(time.Now().UnixNano()) secretNumber := rand.Intn(maxNum) // 打印出要猜测的数字 fmt.Println("The secret number is ", secretNumber) fmt.Println("Please input your guess") // 读信息 reader := bufio.NewReader(os.Stdin) input, err := reader.ReadString('\n') // 捕获读的错误 if err != nil { fmt.Println("An error occured while reading input. Please try again", err) return } // 删除input尾部的‘\n’串与‘\r’串 input = strings.TrimSuffix(input, "\n") input = strings.TrimSuffix(input, "\r") // 字符串转化为数字 guess, err := strconv.Atoi(input) if err != nil { fmt.Println("Invalid input. Please enter an integer value") return } fmt.Println("You guess is", guess) }
v4,可以支持判断输入的数字大了还是小了
package main import ( "bufio" "fmt" "math/rand" "os" "strconv" "strings" "time" ) func main() { maxNum := 100 rand.Seed(time.Now().UnixNano()) secretNumber := rand.Intn(maxNum) fmt.Println("The secret number is ", secretNumber) fmt.Println("Please input your guess") // 读数据 reader := bufio.NewReader(os.Stdin) // 进行读,以\n结尾 input, err := reader.ReadString('\n') if err != nil { fmt.Println("An error occured while reading input. Please try again", err) return } // 去掉字符串结尾的\n与\r,使得字符串整个是数字型字符串 input = strings.TrimSuffix(input, "\n") input = strings.TrimSuffix(input, "\r") guess, err := strconv.Atoi(input) if err != nil { fmt.Println("Invalid input. Please enter an integer value") fmt.Println(err) return } fmt.Println("You guess is", guess) if guess > secretNumber { fmt.Println("Your guess is bigger than the secret number. Please try again") } else if guess < secretNumber { fmt.Println("Your guess is smaller than the secret number. Please try again") } else { fmt.Println("Correct, you Legend!") } } ------------------------------------------ The secret number is 5 Please input your guess --> 23 You guess is 23 Your guess is bigger than the secret number. Please try again
v5,支持循环猜测
package main import ( "bufio" "fmt" "math/rand" "os" "strconv" "strings" "time" ) func main() { maxNum := 100 // 随机种子 rand.Seed(time.Now().UnixNano()) secretNumber := rand.Intn(maxNum) fmt.Println("Please input your guess") reader := bufio.NewReader(os.Stdin) // 带入循环,支持多次猜测数字 for { input, err := reader.ReadString('\n') if err != nil { fmt.Println("An error occured while reading input. Please try again", err) continue } input = strings.TrimSuffix(input, "\n") input = strings.TrimSuffix(input, "\r") guess, err := strconv.Atoi(input) if err != nil { fmt.Println("Invalid input. Please enter an integer value") continue } fmt.Println("You guess is", guess) if guess > secretNumber { fmt.Println("Your guess is bigger than the secret number. Please try again") } else if guess < secretNumber { fmt.Println("Your guess is smaller than the secret number. Please try again") } else { fmt.Println("Correct, you Legend!") break } } } ------------------------------------------------- 输出: Please input your guess --> 23 You guess is 23 Your guess is smaller than the secret number. Please try again --> 45 You guess is 45 Your guess is smaller than the secret number. Please try again