简体   繁体   中英

Subtracting script

I am trying to write a script that counts down any number a user puts in.

Write-Host "$Value1"

[int]$Value1 - [int]1

do {
$outputString = read-host
$outputString - [int]1
}

until ($outputstring=0)

$Value1 defines the number a user puts in. It seems to me that the initial value gets subtracted by 1, and after that the $outputString should take the read-host information and subtract 1 untill it reaches 0. At this point in time the only output I get is the Write-Host " [int]$Value1 - [int]1 ", and the output subtracted by 1. However it does not loop untill it reaches 0. Any way to fix this problem ? Greetings

try :

do {
$outputString = read-host
$outputString -= [int]1
}
until ($outputstring -eq 0)

First , the egal operator is -eq and not =

Second , you need to assign value to $outputstring if you want it to break the loop :

$outputString -= [int]1
$outputString = $outputString - [int]1

How about this?

do {
    $outputString = read-host enter a number
} while ($outputstring -notmatch '^\d+$')

if($outputstring -gt 0)
{
    $outputstring..0 | ForEach-Object{
        $_
        Start-Sleep -Milliseconds 250
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM