我为猜数字游戏写了一个简短的PowerShell脚本。这看起来如下:
clear-host
$Guess = "11"
$response=read-host "Enter your guess"
if ($response -eq $guess)
{
write-host "Congratulations"
}
else {
write-host "Try again"
}
现在,如果我想这样做,经过一定次数的错误猜测,例如3次错误猜测程序将退出。我怎么能这样做?你能不能对这个问题有所了解?
答案 0 :(得分:2)
这是一个带有while循环的解决方案
$count = 0
$answered = $false
while ($count -lt 3 -and $answered -eq $false) {
clear-host
$Guess = "11"
$response=read-host "Enter your guess"
if ($response -eq $guess)
{
$answered = $true
write-host "Congratulations"
}
else {
write-host "Try again"
}
$count++
}