Last active
July 27, 2020 19:25
-
-
Save NicoNekoru/e7f3fb360f8664f8dda9db5f6c703236 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#1 | |
while(!$c) | |
{ | |
try | |
{ | |
[String]$In = Read-Host "Enter a number" | |
$c = $true | |
[BigInt]::Parse($In) > $null | |
} | |
catch | |
{ | |
$c = $false | |
Write-Warning "Please enter an integer" | |
} | |
} | |
#Notes: This is one of best ones but you need to reset $c each time | |
#Better version | |
:cp1 while([String]$In = Read-Host "Enter a number") | |
{ | |
try | |
{ | |
$c = $true | |
[BigInt]::Parse($In) > $null | |
} | |
catch | |
{ | |
$c = $false | |
Write-Warning "Please enter an integer" | |
} | |
if ($c) | |
{ | |
break cp1 | |
} | |
} | |
#The best one | |
#2 | |
while(!$e) | |
{ | |
try | |
{ | |
[int]$In = Read-Host "Enter a number" | |
$e = $true | |
} | |
catch | |
{ | |
$e = $false | |
Write-Warning "Please enter an integer" | |
} | |
} | |
#Will parse empty input as 0, not what intended; also has inegral limit unlike #1. same issue as #1 | |
#3 | |
:loop while ($name = read-host Enter an integer) | |
{ | |
$name -as [int] | findstr $name | |
if ($?) | |
{ | |
[int]$name = $name; break loop | |
} | |
else | |
{ | |
Write-Output "Invalid Input" | |
} | |
} | |
#Slow, not efficient, will also parse blank input as 0 | |
#4 | |
:loop while ($name = read-host Enter an integer){ | |
$ErrorActionPreference = 'SilentlyContinue' | |
[int]$name = $name | |
if (!($name -is [int])) | |
{ | |
echo "Invalid input" | |
} | |
else | |
{ | |
break loop | |
} | |
} | |
#Similar to 3, same issues as 3 and turns ErrorActionPreference to 'SilentlyContinue' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment