【问题标题】:double loop Quit first or repeat双循环 先退出或重复
【发布时间】: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


    【解决方案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
        }
    }
    

    【讨论】:

      【解决方案2】:

      只需做一个 if 语句。如果 response 包含 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')
      

      一种方法

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-04-26
        • 1970-01-01
        • 1970-01-01
        • 2019-08-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多