Powershell没有正确阅读负数

时间:2015-03-31 18:17:38

标签: powershell

我有一个程序,我只是想检查一个负数,但它并不喜欢它。有任何提示吗?

$numbers = @(0,1,2,3,4,5,6,7,8,9,-1,-2,-3,-4,-5,-6,-7,-8,-9)
$number = Read-Host "Please input a number: " 
if ($number -le 0)
    {
        Write-Output "Thanks for using the program! Exitting now..."
        $finished = 1
    }
    elseif ($number -notin $numbers)
    {
        Write-Error "Input must be numeric"
        continue
    }
    else
    {
        Write-Host "Good number!"
    }

1 个答案:

答案 0 :(得分:6)

Read-Host会产生[string]输出,因此您实际上正在进行字符串比较,而不是获得您期望的结果。将$number投射到[int]进行数学比较:

if ([int]$number -le 0)