我正在尝试执行以下操作:
显示供用户输入的选项列表。 所有用户输入选择都是整数(在这种情况下为1、2、3、4和5) 如果用户输入有效选项之一,则应在屏幕上回显他们的输入。 如果用户输入大于给定的选项,则应向用户显示一条消息,即他们的输入大于允许的选项。 如果用户输入的值低于给定的选项,则应向用户显示一条消息,即他们的输入值低于允许的选项。 如果用户输入的不是整数,则应向他们显示一条消息,指出他们的输入不是有效数字。 如果用户输入为空,则应显示一条消息,说明其输入不能为空。 然后,当然,如果出现上面未定义的其他错误,应该告诉他们存在未知错误。
这是我所拥有的,并且大多数情况下都有效。...
cls
echo "What computers are we working on?"
echo ""
echo "1 = All Computer Lab Computers"
echo "2 = Watts' Computer Lab Computers"
echo "3 = Wells' Computer Lab Computers"
echo "4 = A Specific Computer"
echo "5 = Custom list of Computer Lab Computers"
echo ""
echo ""
$empty = $null
$result = $null
[int]$result = read-host "Make a Selection (1 - 5) "
cls
Clear-Host
IF ( $result -lt "6" -and $result -gt "0" ) { echo " You Selected $result" }
ElseIf ( $result -ge "6") {echo "Your Selection of $result was greater than
allowed options" }
ElseIf ( $result -le "0" ) {echo "Your Selection of $result was smaller than
allowed options" }
ElseIf ( $result -eq "") {echo "Your input cannot be blank!" }
ElseIf ( $result -isNot ([int]) ) {echo " Your Selection of $result is not a
valid number. Please try again."}
Else { echo "No clue what happened...but you chose $result"}
但是,如果用户输入非整数或将输入留为空白,则它不会显示预期的消息,而是显示低于允许选项的用户输入消息。 即使用户没有输入任何内容,它也会返回用户输入为“ 0”的
关于如何使这项工作达到预期的任何想法吗?
答案 0 :(得分:1)
这是执行文本菜单的另一种方法... [咧嘴]
$MenuList = @(
'All Computer Lab Computers'
'Watts Computer Lab Computers'
'Wells Computer Lab Computers'
'A Specific Computer'
'Custom list of Computer Lab Computers'
)
$MenuTitle = 'What computers are we working on?'
$ValidChoices = 1..($MenuList.Count)
$Choice = ''
while ([string]::IsNullOrEmpty($Choice))
{
Clear-Host
Write-Host $MenuTitle
foreach ($ML_Item in $MenuList)
{
Write-Host (' {0} - {1}' -f ($MenuList.IndexOf($ML_Item) + 1), $ML_Item)
}
$Choice = Read-Host 'Please select an item from the above list by number or [ x ] to exit '
Write-Host ''
if ($Choice -eq 'x')
{
Write-Host 'Exiting now ...'
break
}
if ($Choice -notin $ValidChoices)
{
[console]::Beep(1000, 300)
Write-Warning (' Your selection [ {0} ] is not valid.' -f $Choice)
Write-Warning ' Please try again ...'
pause
$Choice = ''
}
}
''
'You chose [ {0} ]' -f $Choice
菜单显示...
What computers are we working on?
1 - All Computer Lab Computers
2 - Watts Computer Lab Computers
3 - Wells Computer Lab Computers
4 - A Specific Computer
5 - Custom list of Computer Lab Computers
Please select an item from the above list by number or [ x ] to exit :
输出无效输入...
WARNING: Your selection [ r ] is not valid.
WARNING: Please try again ...
Press Enter to continue...:
有效输入2
...
You chose [ 2 ]
答案 1 :(得分:0)
您可以使用do / until和try / catch使其更加简化,这也将消除对所有if语句的需求
cls
$menu = "What computers are we working on?
1 = All Computer Lab Computers
2 = Watts' Computer Lab Computers
3 = Wells' Computer Lab Computers
4 = A Specific Computer
5 = Custom list of Computer Lab Computers
"
echo $menu
$result = $null
do {
try {
$isValid = $true
[int]$result = read-host "Make a Selection (1 - 5) "
}
catch {$isValid = $false}
Clear-Host
echo "Your Selection of $result is not a valid number. Please try again"
echo ""
echo $menu
} # end do
until (($result -lt 6 -and $result -gt 0) -and $isValid)
cls
Clear-Host
echo " You Selected $result"