【问题标题】:Loop Problems #2 Switch command / Breakin loop循环问题 #2 切换命令/中断循环
【发布时间】:2018-09-16 06:20:28
【问题描述】:

大家好,我仍在尝试找出如何以正确的方式为我的脚本使用循环。

我现在唯一的问题是,当切换开始并且我没有选择任何选项(1,2 或 Q)时,它会跳转到写入“循环完成”的第二部分。

当循环完成一次时我遇到了同样的问题,我被问到是否要使用 R 或 Q 重复或退出。

有没有办法避免这种情况?如果我不使用 1,2,R 或 Q,可能会导致我返回菜单的 break 或 else 命令?

function Show-Menu
{
    ([string]$Title = '? ')
    cls
    Write-Host "================ $Title ================"
    Write-Host "1: Wählen Sie '1' für $F1"
    Write-Host "2: Wählen Sie '2' für $F2"
    Write-Host "Q: Wählen Sie 'Q' zum Beenden."
}

$F1 = "Test1"
$F2 = "Test2"

do
{
    Show-Menu
    $input = Read-Host "Bitte wählen Sie aus."
    switch ($input)
    {
        '1' { 
            $vorname = Read-Host "Bitte Vornamen eingeben:"
            $name = Read-Host "Bitte Namen eingeben:"
            $short = Read-Host "Bitte Kürzel eingeben:"
        }

        '2' {
            $short = Read-Host "Bitte Benutzernamen eingeben:"
            $info = Read-Host -Prompt 'Bitte Beschreibung eingeben.'
        }

        'q' {
            return
        }
    }

    do
    {
        Write-Host "Loop done"
        Clear-Variable -Name vorname,short,name,info
        $response = Read-Host "Drücken Sie R zum Wieder holen, Q zum Beenden" 
        if ($response -eq "Q")
        {
            exit
        }
    } until ($response -eq "R")
} until ($input -eq 'q')

【问题讨论】:

    标签: powershell loops switch-statement break


    【解决方案1】:

    您可以在获取switch 之前检查您的变量,如果没有找到预期值,则可以检查continue

    在 switch 中使用default 块会更优雅地处理此问题,但发现continue/break 适用于最内部的块(如预期的那样)而不是外部的do 块。

    function Show-Menu
    {
      (
               [string]$Title = '? '
         )
         cls
         Write-Host "================ $Title ================"
         Write-Host "1: Wählen Sie '1' für $F1"
         Write-Host "2: Wählen Sie '2' für $F2"
         Write-Host "Q: Wählen Sie 'Q' zum Beenden."
    }
    
    $F1 = "Test1"
    $F2 = "Test2"
    
    do
    {
        Show-Menu
        $input = Read-Host "Bitte wählen Sie aus."
    
        if($input -notin @(1,2,"q")){
            Continue
        }
    
        switch ($input)
        {
            '1' {
                    $vorname = Read-Host "Bitte Vornamen eingeben:"
                    $name = Read-Host "Bitte Namen eingeben:"
                    $short = Read-Host "Bitte Kürzel eingeben:"
                }
            '2' {
                    $short = Read-Host "Bitte Benutzernamen eingeben:"
                    $info = Read-Host -Prompt 'Bitte Beschreibung eingeben.'
                }
            'q' {
                    return
                }
             default {
                # this will be executed if $input does not match another option.
                # continue/break will apply to the switch block, not the do block
             }
        }
    
        do
        {
            Write-Host "Loop done"
            Clear-Variable -Name vorname,short,name,info
            $response = Read-Host "Drücken Sie R zum Wieder holen, Q zum Beenden" 
    
            if($response -notin @("r","q")){
                Continue
            }        
    
            if ($response -eq "Q")
            {
                exit
            }
        }until ($response -eq "R")
    
    }until ($input -eq 'q')
    

    【讨论】:

    • 这解决了第一个问题,已经感谢了。仍然存在的问题是我想从“do”跳回“switch”部分(当我按 R 重复时)。我该如何管理?
    • 我的错误。那是正确的。但不是当我按下随机键时。 R 重复,Q 退出。随机字母只重复“做”部分,它不会回到菜单。
    • 所以要确认你想按随机键与按 R 有相同的行为?在这种情况下,第二个do 循环可以被删除并且代码大大简化。
    猜你喜欢
    • 2018-01-31
    • 1970-01-01
    • 2019-09-29
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    • 2014-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多