Powershell 双循环先退出或重复

Powershell 双循环先退出或重复,powershell,loops,while-loop,do-while,Powershell,Loops,While Loop,Do While,我希望能够在使用选项1和Q后退出脚本。 R表示重复工作,任何其他键都会让我回到 之前的菜单。我可以用另一个while($response-eq“Q”)吗?或者do-while循环在这方面是错误的?我已经试过一个if-else版本,但我做得有点不对。需要帮忙吗 function Show-Menu { param ( [string]$Title = 'Who? ' ) cls Write-Host "================

我希望能够在使用选项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')

只要做一个if语句。如果响应包含字符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')

一种方法就是做一个if语句。如果响应包含字符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'
{
    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
    }
}
那么:

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