我有以下代码将值初始化为 -1:
Set-Variable -Name ID -Value -1 -Scope local
当我这样做时,
$local:ID++
我收到以下错误:
The '++' operator works only on numbers. The operand is a 'System.String'.
我认为它会隐含地将其视为 int。但是,我必须使用一种不整洁的解决方法:
解决方法:
$intVal = [int]$local:ID
$intval ++
还有另一种方法吗?
最佳答案
$id 是一个字符串(键入 $id.gettype() 以查找其类型)。要将其初始化为整数,请将其放在括号中:
PS> Set-Variable -Name ID -Value (-1) -Scope local
PS> ($local:ID++) #increment and print the variable
0
关于powershell - 将 PowerShell 中的整数初始化为负值后递增,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10583986/