在 Go 中,如何保持 for 循环,但如果用户猜测太高或太低则打印不同的输出

In Go, how do stay in a for loop, but print a different output if user guesses too high or too low

我正在尝试在 GO 中开发一个猜谜游戏,让用户尝试 3 次以正确猜出随机数。

它似乎大部分都有效,我正在使用 for 循环来计算剩余的生命数。如果我猜对了数字,它会打印出我想要的:

"You guessed it!"

但问题是,例如,随机数是5。如果我猜 4,它会打印

"Too low"

这是正确的,但在第二次尝试中,如果我要猜测 6,它仍然会说

"Too low"

或者,如果随机数是 5,并且在第一次尝试时我猜到了 6,它会打印

"Too high"

如果我第二次猜 4,它仍然会说

"Too high"

我想我在某个地方陷入了 for 循环,但我不知道如何在不将尝试计数重置回 3 的情况下退出它。

package main

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

func main() {
    secret := getRandomNumber()
    fmt.Println(secret)

    fmt.Println("Guess the number")

    _guess := guessRandomNumber()

    for i := 3; i > 0; i-- {

        if _guess < secret {
            fmt.Println("Too low")
            fmt.Println("You have", i, "guesses left.")
        } else if _guess < secret {
            fmt.Println("Too low")
            fmt.Println("You have", i, "guesses left.")

            guessRandomNumber()
        } else if _guess > secret {
            fmt.Println("Too high")
            fmt.Println("You have", i, "guesses left.")

            guessRandomNumber()
        }

        if _guess == secret {
            fmt.Println("You guessed it!")
            i = 0
        } else if _guess < secret {
            fmt.Println("Too low")
            fmt.Println("You have", i, "guesses left.")

            guessRandomNumber()
        } else if _guess > secret {
            fmt.Println("Too high")
            fmt.Println("You have", i, "guesses left.")

            guessRandomNumber()
        }
    }

}

func getRandomNumber() int {

    rand.Seed(time.Now().UnixNano())
    return rand.Int() % 11
}

func guessRandomNumber() int {
    var guess int
    //fmt.Println("Guess again:")
    fmt.Scanf("%d", &guess)
    return guess
}

如有任何帮助,我们将不胜感激。到目前为止,我真的很喜欢 GO

您没有将函数 guessRandomNumber() 的输出分配给 _guess。除此之外,您还使用了额外的 if-else,这不是必需的。此外,您还向用户提供了 4 次尝试,而不是 3 次。您可以尝试:

package main

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

func main() {
    secret := getRandomNumber()
    fmt.Println(secret)

    fmt.Println("Guess the number")

    var _guess int
    for i := 3; i > 0; i-- {
        _guess = guessRandomNumber()
        if _guess == secret {
            fmt.Println("You guessed it!")
            break
        } else if _guess < secret {
            fmt.Println("Too low")
            fmt.Println("You have", i-1, "guesses left.")
        } else {
            fmt.Println("Too high")
            fmt.Println("You have", i-1, "guesses left.")
        }
        if i != 1 {
            fmt.Println("Guess Again:")
        }
        
    }
}

func getRandomNumber() int {
    rand.Seed(time.Now().UnixNano())
    return rand.Int() % 11
}

func guessRandomNumber() int {
    var guess int
    //fmt.Println("Guess Number:")
    fmt.Scanf("%d", &guess)
    return guess
}

https://onlinegdb.com/aOI-CG4S-

我最终使用带有 switch 语句的 for 循环来解决问题。

package main

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

func main() {
secret := getRandomNumber()
fmt.Println(secret)

fmt.Println("Guess the number")

_guess := guessRandomNumber()

guessCorrect := false
guessCount := 3

for guessCorrect == false && guessCount != 0 {

    switch {
    case _guess < secret:
        fmt.Println("Too low,", guessCount, "guesses left.")
        fmt.Scanf("%d", &_guess)
        guessCount--
        continue
    case _guess > secret:
        fmt.Println("Too high", guessCount, "guesses left.")
        fmt.Scanf("%d", &_guess)
        guessCount--
        continue
    default:
        fmt.Println("You got it!")
        guessCorrect = true
    }

}
}

func getRandomNumber() int {
    rand.Seed(time.Now().UnixNano())
    return rand.Int() % 11
}

func guessRandomNumber() int {
    var guess int
    fmt.Scanf("%d", &guess)
    return guess
}