双循环先退出或重复

时间:2018-04-05 06:28:14

标签: powershell loops while-loop do-while

我希望能够在使用选项1和Q后退出脚本。 R重复作品和任何其他键将带我回到 菜单之前。我可以只使用另一个($ response -eq“Q”)吗?或者是while 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')

2 个答案:

答案 0 :(得分:1)

只需制作一个if语句。如果响应包含Char Q,则返回。

    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")
                  if($response.Contains("Q"))
                  {return}

           } '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')

一种方法

答案 1 :(得分:1)

怎么样:

'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")
    if ($response -eq "q")
    {
        exit
    }
}