Why do integers in PowerShell compare by digits?

后端 未结 4 2035
执念已碎
执念已碎 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:42

    Using a switch:

    [int]$GeneratedNum = Get-Random -min 1 -max 101
    Write-Debug $GeneratedNum
    
    :lop Do{
        switch ((Read-Host 'Take a new guess!') -as [int])
        {
            {$_ -eq $null}{continue}
            {$_ -lt $GeneratedNum}{'Too Low';continue}
            {$_ -gt $GeneratedNum}{'Too High';continue}
            {$true}{'Good Job!';break lop}
        }
    
    } while($true)
    

提交回复
热议问题