Powershell if -lt 问题;如果条件为假则返回真 [英] Powershell if -lt problem; returns true if the condition is false
本文介绍了Powershell if -lt 问题;如果条件为假则返回真的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在使用 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
执行词法与作为 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
返回的字符串转换为数字,以便进行数字比较:
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 问题;如果条件为假则返回真的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文