Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, February 13, 2015 12:39 PM
Hello !
I am kinda confused of the responses of my script.
echo "Number comparison"
do
{
$x = Read-Host "Please enter your first number you want to compare with"
$y = Read-Host "Please enter your second number you want to compare with"
if($x -gt $y)
{
echo "$x is greater than $y."
}
elseif($x -lt $y)
{
Write-Host "$x is lower than $y."
}
else
{
echo "$x is equals $y."
}
}
while (($again = Read-Host "Do you want to do it again?") -eq "No","n","N")
I have an assumption what might be the error, but I'm not really sure if it's possible to save the input of $again either in the brackets or to add {} below while. I tried but nothing was successful.
My objective is to repeat the program when it's finished, to give me/user the possibility to start it again if I type in "Y,y or yes" and stop it when I type "N,n or no". Nothing should happen if there's any other character written except the defined ones.
greetings!
All replies (6)
Friday, February 13, 2015 2:25 PM âś…Answered
Do While example:
Write-Output 'Number comparison'
Do {
$x = Read-Host 'Please enter your first number you want to compare with'
$y = Read-Host 'Please enter your second number you want to compare with'
If($x -gt $y) {
Write-Output "$x is greater than $y."
} ElseIf($x -lt $y) {
Write-Output "$x is lower than $y."
} Else {
Write-Output "$x is equals $y."
}
$again = Read-Host 'Do you want to do it again?'
} While ($again -eq 'Yes' -or $again -eq 'y')
Do Until example:
Write-Output 'Number comparison'
Do {
$x = Read-Host 'Please enter your first number you want to compare with'
$y = Read-Host 'Please enter your second number you want to compare with'
If($x -gt $y) {
Write-Output "$x is greater than $y."
} ElseIf($x -lt $y) {
Write-Output "$x is lower than $y."
} Else {
Write-Output "$x is equals $y."
}
$again = Read-Host 'Do you want to do it again?'
} Until ($again -eq 'No' -or $again -eq 'n')
Don't retire TechNet! - (Don't give up yet - 13,085+ strong and growing)
Friday, February 13, 2015 1:12 PM
From the looks of it, you have your loop condition test backwards.
Instead of Do {} -While(), it should be Do {} -Until(), or if you're going to use While the conditional operator should be -ne.
while (($again = Read-Host "Do you want to do it again?") -ne "No","n","N")
[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
Friday, February 13, 2015 2:12 PM
Thanks for the quick reply.
I just checked it with your suggestion but the output is like:
Please enter your second number you want to compare with: 3
3 is equals 3.
Do you want to do it again?: 2
Please enter your first number you want to compare with: 3
In any case the script is running again - no matter what I type in.
My actual purpose is to run the script again by typing in the correct letters or words. I thought I was right by using the do-while loop because I need to run the script earlier than checking my input (y,Y, etc.)
Is there a possibility to repeat the script afterwards? What do I need to change?
thank you in advance
Friday, February 13, 2015 2:39 PM
Thanks for the quick reply.
I just checked it with your suggestion but the output is like:
Please enter your second number you want to compare with: 3
3 is equals 3.
Do you want to do it again?: 2
Please enter your first number you want to compare with: 3In any case the script is running again - no matter what I type in.
My actual purpose is to run the script again by typing in the correct letters or words. I thought I was right by using the do-while loop because I need to run the script earlier than checking my input (y,Y, etc.)
Is there a possibility to repeat the script afterwards? What do I need to change?
thank you in advance
It's impossible to tell you what you need to change without knowing exactly what you've got.
We can't do much with just "I changed something, but it didn't work. What else do I need to change?"
[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
Friday, February 13, 2015 2:46 PM
function numbercompare
{
[int]$global:x = Read-Host "Please enter your first number you want to compare with"
[int]$global:y = Read-Host "Please enter your second number you want to compare with"
if($global:x -gt $global:y)
{
echo "$global:x is greater than $global:y."
}
elseif($global:x -lt $global:y)
{
Write-Host "$global:x is lower than $global:y."
}
else
{
echo "$global:x is equals $global:y."
}
$global:again = Read-Host "Do you want to do it again?"
}
numbercompare
if ($global:again -notlike "N", "n")
{
numbercompare
}
else
{
Write-Host "End"
exit
}
This is my current script, what else do I have to do (for line 21) to have 3 possibilities:
1st -> User says "Yes", "y" -> program should run again.
2nd-> User says "No", "n" -> program exits/stops.
3rd -> User presses Enter or any other letters except my defined ones to do an action. (It shall repeat as long as the user doesn't type in "yes/y/n/No". How do I do that ?
Friday, February 13, 2015 2:48 PM
This is my current script, what else do I have to do (for line 21) to have 3 possibilities:
1st -> User says "Yes", "y" -> program should run again.
2nd-> User says "No", "n" -> program exits/stops.
3rd -> User presses Enter or any other letters except my defined ones to do an action. (It shall repeat as long as the user doesn't type in "yes/y/n/No". How do I do that ?
See my previous post for an example. The takeaway is to use -or.
Don't retire TechNet! - (Don't give up yet - 13,085+ strong and growing)