嗨,我做了一个数字猜谜游戏,但是我努力使游戏在x个猜谜后停止。
JFrame
答案 0 :(得分:0)
我已经在您的while
循环中添加了一个名为$Tries
的变量,每次有人猜到一个数字,它都会自动递增一个变量,直到等于10
,然后循环中断。 / p>
接下来,我在代码行中包含了告诉您该数字正确的行,并告诉它显示正确的消息并在数字正确时中断循环。
我必须将其包括在循环中,因为如果在$Tries
大于10
时让它中断循环,它仍然会显示消息,指出该数字是正确的。
$Number = (Get-Random 100) + 1
$Guess = 0
$Tries = 0
Write-Host "I'm thinking of a number between 1 and 100."
While ($Number -ne $Guess) {
Write-Host -NoNewline "What is the number? "
$Guess = [int] (Read-Host)
$Tries = $Tries + 1
If ($Tries -gt 10) { Write-Host "You have used all of your guesses, try again." ; break }
If ($Guess -gt $Number) { Write-Host "$Guess is too high." }
If ($Guess -lt $Number) { Write-Host "$Guess is too low." }
If ($Guess -eq $Number) { Write-Host "Correct! $Number is the number I was thinking!" ; break}
}