本文介绍了Powershell if -lt问题;如果条件为假,则返回true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对Powershell中的小脚本有疑问.这是我的脚本:
I have a problem with small script in Powershell.Here is my script:
$number = Read-Host "Enter a number"
if ($number -lt 3){
Write-Host "Number is too low."
break
}
但是,例如,当我输入25
时,if
条件仍然为true.
But when I enter 25
, for example, the if
conditional still evaluates to true.
推荐答案
Read-Host
始终返回 string ,而-lt
对字符串进行 lexical 比较为LHS:
Read-Host
always returns a string, and -lt
performs lexical comparison with a string as the LHS:
PS> '25' -lt 3
True # because '2' comes lexically before '3'
为了执行数字比较,必须将从Read-Host
返回的字符串转换为 number :
You must convert the string returned from Read-Host
to a number in order to perform numerical comparison:
[int] $number = Read-Host "Enter a number"
if ($number -lt 3){
Write-Host "Number is too low."
break
}
这篇关于Powershell if -lt问题;如果条件为假,则返回true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!