代码之家  ›  专栏  ›  技术社区  ›  peter

检查用户输入是否为数字,然后从切换到打印

  •  3
  • peter  · 技术社区  · 6 年前

    代码:

    [int]$s = Read-Host "Enter number from 1-3"
    switch ($s) {
      1 { $s = 'Apple' }
      2 { $s = 'Melon' } 
      3 { $s = 'Mango' }
    } 
    $s 
    

    输出:

    无法将值“Apple”转换为类型“System.Int32”。错误 输入的格式不正确。

    所以我的问题是:我如何检查我的输入是否是一个数字,同时从我的开关中选择?

    5 回复  |  直到 6 年前
        1
  •  2
  •   Mark Wragg    6 年前

    代码的问题是,在定义 $s 作为一个整数,您稍后将尝试为其分配一个字符串值。

    您可以这样做:

    [int]$s = Read-Host "Enter number from 1-3"
    
    $result = switch ($s) {
      1 { 'Apple' }
      2 { 'Melon' } 
      3 { 'Mango' }
    } 
    
    $result
    

    注意,我还通过将切换结果返回到 $result 而不是在每个条件中分配它。

    这是因为 $result 是一个未定义的变量,当您为其赋值时,该变量将变为字符串。

    如果要验证输入是否为整数,还可以考虑执行以下操作:

    $input = Read-Host "Enter number from 1-3"
    
    if (($input -isnot [int])) { Throw 'You did not provide a number as input' }
    
    $result = switch ($input) {
      1 { 'Apple' }
      2 { 'Melon' } 
      3 { 'Mango' }
    } 
    
    $result
    
        2
  •  2
  •   Victor Silva    6 年前

    不需要使用整数来声明变量:

    $s = Read-Host "Enter number from 1-3"
    switch ($s) {
      1 { $s = 'Apple' }
      2 { $s = 'Melon' } 
      3 { $s = 'Mango' }
    } 
    $s
    

    如果检查变量类型:

    enter image description here

        3
  •  1
  •   boxdog    6 年前

    当您有这样一组预定义的选项时,请考虑使用多选菜单,如下所示:

    $title = "Select Fruit"
    $prompt = "Which fruit is your favorite?"
    $apple = New-Object System.Management.Automation.Host.ChoiceDescription "&Apple","Apple"
    $melon= New-Object System.Management.Automation.Host.ChoiceDescription "&Melon","Melon"
    $mango= New-Object System.Management.Automation.Host.ChoiceDescription "Man&go", "Mango"
    
    $options = [System.Management.Automation.Host.ChoiceDescription[]]($apple, $melon, $mango)
    
    $selectedFruit = $host.ui.PromptForChoice($title, $prompt, $options, 0) 
    
    switch($selectedFruit)
    {
        0 {Write-Host "You chose Apple"}
        1 {Write-Host "You chose Melon"}
        2 {Write-Host "You chose Mango"}
    }
    

    在ISE中,用户将看到一个GUI提示,带有可单击的按钮,在控制台上,还有一个菜单,其中包含可选择的特定允许字母(本例中为a、M和G)。

    此方法的优点是,它看起来像来自PowerShell的典型提示,如果用户输入无效值,它将检查并重新提示。您可以添加“退出”选项,这样用户就可以轻松跳过所有选项。

        4
  •  0
  •   Vivek Kumar Singh    6 年前

    如何检查输入是否为数字,同时从开关中选择?

    How do you check if your input is a number? -我认为您已经在这样做了,因为您正在声明变量 $s [int] 一开始-

    [int]$s = Read-Host "Enter number from 1-3"
    

    Selecting from your switch -您得到的错误是由于输入到 read-host 和变量 $s . $s 显然是一个整数,当您为其分配字符串时。因此,您必须再次键入cast才能更正该错误-

    [int]$s = Read-Host "Enter number from 1-3"
    switch ($s) {
    1 { [string]$s = 'Apple' }
    2 { [string]$s = 'Melon' } 
    3 { [string]$s = 'Mango' }
    } 
    $s 
    

    如果输入除1、2或3以外的任何数字,则 $s 将在其中存储该号码。例如,如果输入5, $s 将在其中存储5,因为 switch 声明尚未执行。

        5
  •  0
  •   Richard-Degenne    4 年前
    function Verify-InputInteger {
        Param (
            $Question="Saisir un nombre",
            $Min=0,
            $Max=100
        )
    
        try {
            [int]$string_Input = Read-Host $Question;
            
            if($string_Input -LT $Min)
            {
                Throw "ErrorMin"
            }
            if($string_Input -GT $Max)
            {
                Throw "ErrorMax"
            }        
            return $string_Input;
        }
        catch {
    
            if($_.FullyQualifiedErrorId -EQ 'InvalidCastFromStringToInteger')
            {
                Write-Host 'You did not provide a number as input' -ForegroundColor Red;
                Verify-InputInteger -Question $Question -Min $Min -Max $Max;
            }    
            if($_.Exception.Message -EQ 'ErrorMin')
            {
                Write-Host 'You have entered a number lesser than the minimum limit' -ForegroundColor Red;
                Verify-InputInteger -Question $Question -Min $Min -Max $Max;
            }   
            if($_.Exception.Message -EQ 'ErrorMax')
            {
                Write-Host 'You have entered a number greater than the maximum limit' -ForegroundColor Red;
                Verify-InputInteger -Question $Question -Min $Min -Max $Max;
            }
        }
    }