go猜数字游戏
所属分类 go
浏览量 825
go猜数字游戏
go run guess.go
package main
import (
"bufio"
"fmt"
"log"
"math/rand"
"os"
"strconv"
"strings"
"time"
)
func main() {
seconds := time.Now().Unix()
rand.Seed(seconds)
target := rand.Intn(100) + 1
reader := bufio.NewReader(os.Stdin)
success := false
for guesses := 0; guesses < 10; guesses++ {
fmt.Print("Please guess a magic number:")
input, err := reader.ReadString('\n')
if err != nil {
log.Fatal(err)
}
input = strings.TrimSpace(input)
guess, err := strconv.Atoi(input)
if err != nil {
log.Fatal(err)
}
if guess < target { //
fmt.Println("Wrong!Too small!")
} else if guess > target {
fmt.Println("Wrong!Too big!")
} else {
success = true
fmt.Println("Right!")
break
}
}
if !success {
fmt.Println("Sorry, you didn't guess my number. it was", target)
}
}
java 猜数字
C语言猜数字
go http server
上一篇
下一篇
c语言整数进制
C语言猜数字
java 猜数字
Rust安装
python猜数字小游戏
javascript猜数字小游戏