英文:
In Go, how do stay in a for loop, but print a different output if user guesses too high or too low
问题
我正在尝试在GO中开发一个猜数字游戏,给用户三次机会来猜测正确的随机数。
它似乎大部分都工作正常,我使用for循环来计算剩余的生命次数。如果我猜对了数字,它会打印我想要的结果:
"你猜对了!"
但问题是,例如,随机数是5。如果我猜4,它会打印
"太低了"
这是正确的,但是在第二次尝试中,如果我猜6,它仍然会说
"太低了"
或者,如果随机数是5,在第一次尝试中我猜6,它会打印
"太高了"
如果我在第二次尝试中猜4,它仍然会说
"太高了"
我觉得我在某个地方陷入了for循环,但我不知道如何在不重置尝试次数的情况下退出它。
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
secret := getRandomNumber()
fmt.Println(secret)
fmt.Println("猜数字")
_guess := guessRandomNumber()
for i := 3; i > 0; i-- {
if _guess < secret {
fmt.Println("太低了")
fmt.Println("你还有", i, "次猜测机会。")
} else if _guess < secret {
fmt.Println("太低了")
fmt.Println("你还有", i, "次猜测机会。")
guessRandomNumber()
} else if _guess > secret {
fmt.Println("太高了")
fmt.Println("你还有", i, "次猜测机会。")
guessRandomNumber()
}
if _guess == secret {
fmt.Println("你猜对了!")
i = 0
} else if _guess < secret {
fmt.Println("太低了")
fmt.Println("你还有", i, "次猜测机会。")
guessRandomNumber()
} else if _guess > secret {
fmt.Println("太高了")
fmt.Println("你还有", i, "次猜测机会。")
guessRandomNumber()
}
}
}
func getRandomNumber() int {
rand.Seed(time.Now().UnixNano())
return rand.Int() % 11
}
func guessRandomNumber() int {
var guess int
//fmt.Println("再次猜测:")
fmt.Scanf("%d", &guess)
return guess
}
任何帮助将不胜感激。到目前为止,我真的很喜欢GO。
英文:
I'm trying to develop a guessing game in GO that gives the user three attempts to guess the random number correctly.
It mostly seems to work, I'm using a for loop to count the amount of lives left. If I guess the number correctly, it prints what I want:
> "You guessed it!"
But the issue is, say for example, the random number is 5. If I guessed 4, it would print
> "Too low"
which is correct, but in the second attempt if I was to guess 6, it would still say
> "Too low"
Alternatively, if the random number was 5, and on the first attempt I guessed 6, it would print
> "Too high"
If I was to guess 4 on the second attempt, it would still say
> "Too high"
I think I'm getting caught in the for loop somewhere, but I don't know how to exit it without resetting the attempts count back to 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
}
Any help would be greatly appreciated. I'm really liking GO so far.
答案1
得分: 3
你没有将函数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-
英文:
You're not assigning the output of your function guessRandomNumber()
to your _guess
. Apart from this also you're using extra if-else
, which are not required. Also you're giving 4 attempts to user instead of 3. You can try:
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
}
答案2
得分: 0
我使用了一个带有switch语句的for循环来解决这个问题。
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
secret := getRandomNumber()
fmt.Println(secret)
fmt.Println("猜数字")
_guess := guessRandomNumber()
guessCorrect := false
guessCount := 3
for guessCorrect == false && guessCount != 0 {
switch {
case _guess < secret:
fmt.Println("太低了,还有", guessCount, "次猜测机会。")
fmt.Scanf("%d", &_guess)
guessCount--
continue
case _guess > secret:
fmt.Println("太高了,还有", guessCount, "次猜测机会。")
fmt.Scanf("%d", &_guess)
guessCount--
continue
default:
fmt.Println("你猜对了!")
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
}
英文:
I ended up using a for loop with a switch statement to solve the issue.
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
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论