Why do integers in PowerShell compare by digits?

后端 未结 4 2030
执念已碎
执念已碎 2020-11-30 15:05

My code tells you whether your guessed number is higher or lower than a randomly generated number, but it seems to only compare the first digits of the number when one of th

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-30 15:26

    This is because you're comparing a string to an integer. The order matters.

    "56" -lt 7
    

    Is actually the same as:

    "56" -lt "7"
    

    Alternatively:

    56 -lt "7"
    

    would give you the correct result. PowerShell tries to coerce the right side argument to the type of the left side.

    You might try an explicit cast:

    [int]$Input -lt $GeneratedNum
    

提交回复
热议问题