简体   繁体   English

减法脚本

[英]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 ? $Value1定义了用户输入的数字。在我看来,初始值减去 1,然后$outputString应该获取读取主机信息并减去 1,直到它达到 0。此时我得到的唯一输出是写入主机“ [int]$Value1 - [int]1 ”,并且输出减去 1。但是它直到达到 0 才会循环。有什么办法可以解决这个问题? Greetings你好

try :尝试 :

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

First , the egal operator is -eq and not =首先,egal 运算符是-eq而不是=

Second , you need to assign value to $outputstring if you want it to break the loop :其次,如果您希望它打破循环,您需要为$outputstring赋值:

$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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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