【发布时间】:2018-04-05 06:28:13
【问题描述】:
我希望在使用选项 1 和 Q 后能够退出脚本。 R 表示重复工作,任何其他键都会让我回到 之前的菜单。我可以再使用一个 while ($response -eq "Q") 吗?还是 do while 循环是错误的?我已经尝试过 if else 版本,但我做错了。有什么帮助吗?
function Show-Menu
{
param (
[string]$Title = 'Who? '
)
cls
Write-Host "================ $Title ================"
Write-Host "1: 1"
Write-Host "2: 2"
Write-Host "3: 3"
Write-Host "4: 4"
Write-Host "5: 5"
Write-Host "6: 6"
Write-Host "7: 7"
Write-Host "8: 8"
Write-Host "Q: Q to Quit."
}
do
{
Show-Menu
$input = Read-Host "Please choose."
switch ($input)
{
'1' {
cls
'You chose option #1'
do {
$response = Read-Host "R to repeat, Q to Quit or anything else to go back" }
while ($response -eq "R")
} '2' {
cls
'You chose option #2'
} '3' {
cls
'You chose option #3'
} 'q' {
return
} '3' {
cls
'You chose option #1'
} '4' {
cls
'You chose option #2'
} '' {
cls
'You chose option #3'
} 'q' {
return
}
}
pause
}
until ($input -eq 'q')
【问题讨论】:
标签: powershell loops while-loop do-while