You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

循环问题#2:Switch命令/中断循环——无效输入需返回菜单

Fixing Your PowerShell Loop Input Validation Issue

Got it, let's tackle this loop problem head-on. The core issue here is that your current script doesn't handle invalid input by looping back to the menu—it just falls through to the "loop Done" section. We can fix this with nested do-while loops and switch statement default branches to enforce input validation at every step.

Here's a revised version of your script structure that addresses both problems:

function Show-Menu {
    Write-Host "=== Main Menu ==="
    Write-Host "1. Option 1"
    Write-Host "2. Option 2"
    Write-Host "Q. Quit"
    Write-Host "-----------------"
    return Read-Host "Please select an option"
}

# Outer loop to control whether the entire program repeats
do {
    # Inner loop to force valid menu input (1/2/Q)
    do {
        $menuChoice = Show-Menu
        switch ($menuChoice.ToUpper()) {
            '1' {
                Write-Host "Running Option 1 logic..."
                # Add your actual Option 1 code here
                break # Exit this switch to move to the repeat prompt
            }
            '2' {
                Write-Host "Running Option 2 logic..."
                # Add your actual Option 2 code here
                break # Exit this switch to move to the repeat prompt
            }
            'Q' {
                Write-Host "Exiting program immediately..."
                exit # Terminate the script entirely
            }
            default {
                Write-Host "Oops! That's not a valid option. Please enter 1, 2, or Q." -ForegroundColor Red
                # No break here—let the inner do-while loop repeat the menu
            }
        }
    } while ($menuChoice.ToUpper() -notin @('1', '2', 'Q')) # Repeat until valid input

    # Inner loop to force valid repeat/quit input (R/Q)
    do {
        $repeatChoice = Read-Host "Do you want to run again? (R/Q)"
        switch ($repeatChoice.ToUpper()) {
            'R' {
                Write-Host "Restarting the menu..."
                $continueLoop = $true
                break # Exit this switch to restart the outer loop
            }
            'Q' {
                Write-Host "Wrapping up—loop done!"
                $continueLoop = $false
                break # Exit this switch to end the outer loop
            }
            default {
                Write-Host "Invalid input! Please enter R (repeat) or Q (quit)." -ForegroundColor Red
                # No break here—let the inner do-while loop repeat the prompt
            }
        }
    } while ($repeatChoice.ToUpper() -notin @('R', 'Q')) # Repeat until valid input

} while ($continueLoop)

Key fixes explained:

  • Nested do-while loops: We use inner loops for both menu selection and repeat confirmation. These loops only exit when the user enters a valid input (1/2/Q for the menu, R/Q for the repeat prompt).
  • switch default branches: These catch any invalid input, show a clear error message, and let the inner loop repeat the prompt/menu instead of falling through to the end.
  • Explicit loop control: The outer do-while relies on a $continueLoop variable to decide whether to restart the program, ensuring we only exit when the user explicitly chooses Q.

This structure ensures that invalid input never sends the script straight to "loop Done"—it keeps the user in the current selection step until they enter a valid option.

内容的提问来源于stack exchange,提问作者batchn00b

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:21:46