0

我正在为颜色猜谜游戏在 PowerShell 中编写脚本。计算机随机选择一种颜色,然后玩家尝试猜测颜色。我让它工作起来,直到我切换脚本中的一些代码行以试图让这两个变量正确显示。现在,我无法让代码运行超过玩家声明他们是否想玩游戏的点。我当前的错误是 do 循环,控制台看不到我有一个 while 循环,因此会引发错误并且不会运行其余代码。

我设法使用 Visual Studio Code 的格式文档功能修复了格式,但我仍然无法解决这个 while 循环问题。

Write-Host ''; 'Hello again my friend!'; ''
$name = Read-Host "What is your name?"
Write-Host ''; "It's good to see you again, $name! Would you like to guess my favorite color?"; ''
$command = Read-Host @'
"How do you answer? (Yes or No?)"
1. Yes (y)
2. No (n)
3. Quit (q)
Enter Choice
'@
switch -Wildcard ($command) {
    'Y' { 'Yes!'; '' } 
    { $_ -eq 'y' -or $_ -like 'Ye*' } {
        Write-Host "This will be fun! Let us begin!"
        Break
    }
    'N' { 'No!'; '' }
    { $_ -eq 'n' -or $_ -like 'No*' } {
        Write-Host "That's too bad, perhaps another day!"
        Exit
    }
    'Q' { 'Quit'; '' }
    { $_ -eq 'q' -or $_ -like 'qu*' } {
        Write-Host 'So long!'
        Exit
    }
    default {
        'Invalid Command, Please start over.'
        Exit
    }
}

[string]$playagain = 'y'
[int]$playerwins = 0
[int]$compwins = 0
[int]$totalguesses = 0
[int]$playergames = 0
[int]$compgames = 0
[int]$round = 1
[int]$game = 1
$cpuchoice = $color


while ($playagain -eq 'y') { 
    Write-Host ''; "Game $game!"; ''
    $cpuchoice = @([System.Enum]::GetValues([System.ConsoleColor])) | Get-Random -Count 1
        
    do {
        Write-Host "Round $round! What is my favorite color?"; ''
        $listcolor = Read-Host "Would you like to see a list of available colors? Choose 'y' for yes, and 'n' for no."
        if ($listcolor -eq 'y') {
            [System.Enum]::GetValues([System.ConsoleColor])
        }
        elseif ($listcolor -eq 'n') {
            Write-Host "Suit yourself, let's start."
        }
        else {
            Write-Host "Your choice was invalid. Please choose 'y' for yes, or 'n' for no."
        }
        do {
            $playerchoice = Read-host "Enter your guess"
        } while (([System.Enum]::GetValues([System.ConsoleColor])) -notcontains $playerchoice) {

            if ($playerchoice -eq $cpuchoice ) {
                Write-Host "You win, my favorite color is $cpuchoice." -ForegroundColor $cpuchoice; ''
                $playerwins = $playerwins + 1
                $totalguesses = $totalguesses + 1
            }
            elseif ($playerchoice -ne $cpuchoice ) {
                Write-Host "You lose, try again."; ''
                $playerguesses += $playerchoice
                $playerguesses = $playerguesses.Split(',')
                $totalguesses = $totalguesses + 1
                Write-Host "Here are your guesses so far: "
                $playerguesses
                ''
            } 
            $round = $round + 1         
        }                               
        until($playerwins -eq 1) {
            $playergames = $playergames + 1
            Write-Host "You've won this round and have won $playergames games." -ForegroundColor Green
            Write-Host "Your total guesses: $totalguesses."
            Write-Host "Your wins - $playergames" -ForegroundColor Yellow
            Write-Host "Computer wins - $compgames" -ForegroundColor Yellow
            ''
        }
        $playagain = Read-Host "I enjoyed this game. Would you like to challenge again, $name? Y or N"
        while (("y", "n") -notcontains $playagain) {
            if ($playagain -eq "y") {
                Write-Host "I look forward to our next battle!"; ''
                $playerwins = 0
                $compwins = 0
                $game = $game + 1
            }
            elseif ($playagain -eq "n") {
                Write-Host "Thank you for playing!"
                exit
            }
        }
    }        
}                                 

导致错误的 do 循环是在第一个 while 语句之后以“Write-Host "Round $round!" 开头的循环。

任何帮助表示赞赏!谢谢!

4

1 回答 1

1

Your program is being parsed like this:

while ($playagain -eq 'y')
{ 

    # do loop #1
    do
    {

        # do loop #2
        do {
        }
        while (([System.Enum]::GetValues([System.ConsoleColor])) -notcontains $playerchoice)

        # floating script block #1
        # (doesn't execute the scriptblock, but it gets sent to the output stream instead)
        {
           if ($playerchoice -eq $cpuchoice ) {
           ... etc ...
        }

        # try to invoke a cmdlet "until" with 2 parameters
        # i.e. ($playerwins -eq 1) and { ... }
        until ($playerwins -eq 1) {
           $playergames = $playergames + 1
           ... etc ...
        }

        # while loop #1
        while (("y", "n") -notcontains $playagain) {
           ...
        }

    }
    
}

The error is telling you the first do (do loop #1) doesn't have a trailing while or until.

There's no clear and simple fix I can offer to make your code run short of a significant rework because there's a number of issues (e.g. the floating script block #1, the dangling until and the while \ until-less do), but that's what the current error is saying anyway...

于 2022-02-04T00:06:41.537 回答