且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在数组中添加数字总和

更新时间:2023-02-10 20:31:42

变量$ input是保留的powershell变量(

The variable $input is a reservd powershell variable (take a look here) and you should not use it in this context.
As in the comments mentiond I would also put an else in your loop and save the input to an array.

[System.Int32[]]$UserInputs = @() #INT Array
do {

    [System.Int32]$UserInput = Read-Host -Prompt 'Enter variety of numbers and press 0 to add them together'

    if ($UserInput -eq 0)
    {
        Out-Host -InputObject ('Sum of numbers entered ' + ($UserInputs | Measure-Object -Sum).Sum) #Return the sum
    }
    else
    {
        $UserInputs += $UserInput #Add user input to array
    }
}
while ($UserInput -ne 0)