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

huangapple go评论85阅读模式
英文:

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 (
&quot;fmt&quot;
&quot;math/rand&quot;
&quot;time&quot;
)
func main() {
secret := getRandomNumber()
fmt.Println(secret)
fmt.Println(&quot;Guess the number&quot;)
_guess := guessRandomNumber()
for i := 3; i &gt; 0; i-- {
if _guess &lt; secret {
fmt.Println(&quot;Too low&quot;)
fmt.Println(&quot;You have&quot;, i, &quot;guesses left.&quot;)
} else if _guess &lt; secret {
fmt.Println(&quot;Too low&quot;)
fmt.Println(&quot;You have&quot;, i, &quot;guesses left.&quot;)
guessRandomNumber()
} else if _guess &gt; secret {
fmt.Println(&quot;Too high&quot;)
fmt.Println(&quot;You have&quot;, i, &quot;guesses left.&quot;)
guessRandomNumber()
}
if _guess == secret {
fmt.Println(&quot;You guessed it!&quot;)
i = 0
} else if _guess &lt; secret {
fmt.Println(&quot;Too low&quot;)
fmt.Println(&quot;You have&quot;, i, &quot;guesses left.&quot;)
guessRandomNumber()
} else if _guess &gt; secret {
fmt.Println(&quot;Too high&quot;)
fmt.Println(&quot;You have&quot;, i, &quot;guesses left.&quot;)
guessRandomNumber()
}
}
}
func getRandomNumber() int {
rand.Seed(time.Now().UnixNano())
return rand.Int() % 11
}
func guessRandomNumber() int {
var guess int
//fmt.Println(&quot;Guess again:&quot;)
fmt.Scanf(&quot;%d&quot;, &amp;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 (
&quot;fmt&quot;
&quot;math/rand&quot;
&quot;time&quot;
)
func main() {
secret := getRandomNumber()
fmt.Println(secret)
fmt.Println(&quot;Guess the number&quot;)
var _guess int
for i := 3; i &gt; 0; i-- {
_guess = guessRandomNumber()
if _guess == secret {
fmt.Println(&quot;You guessed it!&quot;)
break
} else if _guess &lt; secret {
fmt.Println(&quot;Too low&quot;)
fmt.Println(&quot;You have&quot;, i-1, &quot;guesses left.&quot;)
} else {
fmt.Println(&quot;Too high&quot;)
fmt.Println(&quot;You have&quot;, i-1, &quot;guesses left.&quot;)
}
if i != 1 {
fmt.Println(&quot;Guess Again:&quot;)
}
}
}
func getRandomNumber() int {
rand.Seed(time.Now().UnixNano())
return rand.Int() % 11
}
func guessRandomNumber() int {
var guess int
//fmt.Println(&quot;Guess Number:&quot;)
fmt.Scanf(&quot;%d&quot;, &amp;guess)
return guess
}

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

答案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 (
&quot;fmt&quot;
&quot;math/rand&quot;
&quot;time&quot;
)
func main() {
secret := getRandomNumber()
fmt.Println(secret)
fmt.Println(&quot;Guess the number&quot;)
_guess := guessRandomNumber()
guessCorrect := false
guessCount := 3
for guessCorrect == false &amp;&amp; guessCount != 0 {
switch {
case _guess &lt; secret:
fmt.Println(&quot;Too low,&quot;, guessCount, &quot;guesses left.&quot;)
fmt.Scanf(&quot;%d&quot;, &amp;_guess)
guessCount--
continue
case _guess &gt; secret:
fmt.Println(&quot;Too high&quot;, guessCount, &quot;guesses left.&quot;)
fmt.Scanf(&quot;%d&quot;, &amp;_guess)
guessCount--
continue
default:
fmt.Println(&quot;You got it!&quot;)
guessCorrect = true
}
}
}
func getRandomNumber() int {
rand.Seed(time.Now().UnixNano())
return rand.Int() % 11
}
func guessRandomNumber() int {
var guess int
fmt.Scanf(&quot;%d&quot;, &amp;guess)
return guess
}

huangapple
  • 本文由 发表于 2022年5月19日 11:51:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/72298414.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定