PowerShell嵌套do...while循环无法同时终止问题求助
问题
我正在编写一个用于练习PowerShell技能的小程序,外层do...while循环运行正常,内层do...while循环也能执行,但内层循环满足条件后无法同时终止两个循环,会回到“Are you in a hurry? y/n:”提示。我尝试过调整break语句的位置以及使用标签,都没能得到预期结果。我知道可能有更优的写法,但目前希望按自己的思路解决问题,不进行代码重写(除非需求无法实现)。
附代码如下:
$resultArray = @("go", "not go") # give result $result = $resultArray | Get-Random # text results to jason # check if in hurry do{ while ($get_in_hurry = read-host "Are you in a hurry? y/n:"){ # if in hurry, jump to spin if ($get_in_hurry -eq "y") { Write-Host "You seleted you are in a hurry." Write-Host "My suggestion is to" $result "to poker with Jason tonight." break } # if not in hurry ask questions elseif ($get_in_hurry -eq "n") { Write-Host "You seleted you are NOT in a hurry." # check mood do{ while ($get_mood = read-host "Are you in a good mood? y/n:") { # if not in good mood, adjust percentage, and spin if ($get_mood -eq "n") { Write-Host "You seleted you are not in a good mood." Write-Host "Write something funny" $resultArray += "not go", "not go", "not go", "not go", "not go", "not go", "not go", "not go" Write-Host "My suggestion is to" $result "to poker with Jason tonight." break } # good mood, adjust percentage elseif ($get_mood -eq "y") { Write-Host "You seleted you are in a good mood." break } # if mood not yes or no, request yes or no else { Write-Host "Please speak in english. Are in a good mood? y/n:" } } # gone to poker in last month, adjust percentage # jason ask to go in last two weeks, adjust percentage # did you go, adjust percentage # do math } while (($get_mood -eq $null) -or ($get_mood -eq "")) } # if hurry not yes or no, request yes or no else { Write-Host "Please speak in english. Are in a hurry. y/n:" # save variable text to jason to update application to give better response to any funny answers that may be given } } } # if hurry null, request yes or no while (($get_in_hurry -eq $null) -or ($get_in_hurry -eq ""))
解决方案
你之前用标签没成功,应该是标签的使用方式不对。PowerShell支持带标签的break语句,能直接跳出指定的外层循环,完全不用重写整体结构,只需要做以下几处修改:
- 给最外层的do循环加上标签(比如
:OuterLoop),放在do的前面 - 在内层循环需要终止所有循环的分支里,用
break OuterLoop替代普通的break
修改后的关键代码片段如下:
# 给外层do循环添加标签 :OuterLoop do{ while ($get_in_hurry = read-host "Are you in a hurry? y/n:"){ # if in hurry, jump to spin if ($get_in_hurry -eq "y") { Write-Host "You seleted you are in a hurry." Write-Host "My suggestion is to" $result "to poker with Jason tonight." # 直接跳出外层循环 break OuterLoop } # ... 中间原有代码保留 ... # if not in good mood, adjust percentage, and spin if ($get_mood -eq "n") { Write-Host "You seleted you are not in a good mood." Write-Host "Write something funny" $resultArray += "not go", "not go", "not go", "not go", "not go", "not go", "not go", "not go" Write-Host "My suggestion is to" $result "to poker with Jason tonight." # 直接跳出外层循环 break OuterLoop } # good mood, adjust percentage elseif ($get_mood -eq "y") { Write-Host "You seleted you are in a good mood." # 按需求,选完y后也终止所有循环,所以改成带标签的break break OuterLoop } # ... 其他原有代码保留 ... } # if hurry null, request yes or no while (($get_in_hurry -eq $null) -or ($get_in_hurry -eq ""))
修改说明:
- 标签格式必须是
标签名 + 空格 + do{...},比如:OuterLoop do{ - 当需要同时终止内外层循环时,用
break 标签名,PowerShell会直接跳转到标签所在循环的外部,不会回到外层的输入提示 - 如果是输入非法需要继续当前内层提问的场景,保留普通
break即可
这样修改完全贴合你不想重写代码的需求,只是添加了标签和修改break语句,就能解决内层循环结束后回到外层输入的问题。
内容的提问来源于stack exchange,提问作者Jason
相关产品推荐
相关产品推荐

