【问题标题】:Check if user input is a number and then select from switch to print检查用户输入是否为数字,然后从开关中选择打印
【发布时间】:2018-05-17 10:42:50
【问题描述】:

代码:

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

输出:

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

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

【问题讨论】:

  • 为什么要为两种不同的类型重用$s 变量?为什么不只是1 { 'Apple' }...?
  • 对这一切有点陌生......所以不知道该怎么做

标签: powershell input scripting numbers


【解决方案1】:

您的代码的问题是,在您将$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】:

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

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

    如果你检查变量的类型:

    【讨论】:

    • 这仍然会导致错误发生,因为$s一旦通过给它一个整数值被定义为一个整数就不能被分配一个字符串。
    • 你说得对,它在我的 ISE 窗口中不起作用,因为在之前的执行中它已经被强输入为 [int],但在新的 powershell 会话中它确实可以正常工作。我会支持你:)
    • 尝试输入一个字母
    • @MarkWragg 没问题的人!你只是让我重新思考答案
    • @peter 如果您使用字母,结果将是输入的字母
    【解决方案3】:

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

    $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】:

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

      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 显然是一个整数,当您为其分配一个字符串时。因此,错误您必须再次进行类型转换才能更正该错误 -

      [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】:
        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;
                }
            }
        }
        

        【讨论】:

        • 嗨,欢迎来到 StackOverflow!答案不应该是简单的 sn-ps 代码。你能详细说明一下这段代码为什么以及如何回答这个问题吗?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-27
        • 1970-01-01
        • 2019-05-23
        • 2011-06-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多