在 Windows PowerShell 中提示並接受輸入

在 Windows PowerShell 中提示並接受輸入

在問答腳本中,您將如何輸入填空問題和答案,例如《初學者程式設計》中的《宋飛傳》問答遊戲?

答案1

Read-Host你可以直接使用System.Console類,讀取單一字元(無需 ENTER):

$key = [Console]::ReadKey()
$char = $key.KeyChar

答案2

有的是讀取主機 cmdlet用於讀取用戶的輸入,您需要一些條件邏輯檢查正確答案。

答案3

#Ask the player the seventh question
while (($question7 -eq "")) {

  Clear-Host  #Clear the Windows command console screen

  Write-Host
  Write-Host " What food item did Jerry say Newman wouldn't eat even if it was deep fried in chocolate sauce?"
  Write-Host
  Write-Host " Broccoli"
  Write-Host " Peas"
  Write-Host " Apples"
  Write-Host " Carrots"
  Write-Host
  $question7 = Read-Host " Type the word representing the correct answer" `
    "and press the Enter key"

}

相關內容