在 Windows PowerShell 中提示和接受输入

在 Windows PowerShell 中提示和接受输入

在琐事脚本中,您如何输入填空问题和答案,就像《初学者编程》中的 Seinfeld 琐事游戏一样?

答案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"

}

相关内容