我在其中一个PowerShell脚本中遇到了一种奇怪的行为。 我在那个剧本中使用它:
$GUI_ProgressBar.Value = (($n)/($f_count*$d_count*$w_count)*100)
$n++
Write-Host "f_count= "$f_count
Write-Host "d_count= "$d_count
Write-Host "w_count= "$w_count
Write-Host "Bruch= "($f_count*$d_count*$w_count)
Write-Host "n= "$n
该部分的输出(在几个循环之后)是:
f_count= 1
d_count= 17
w_count= 6
Bruch= 111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
n= 52
直接在shell中使用时,代码工作正常。
任何想法为什么“布鲁赫”是这样的?
答案 0 :(得分:1)
我认为$f_count
可能是一个字符串。引用here,因为这实际上是PowerShell的一项功能。如果左侧运算符是右侧乘以int的字符串,则该字符串将作为右侧运算符的值重复。
如果您需要$ f_count在某些区域中成为字符串,但是对于此特定要求,需要它为int
值,要么强制转换它([int]$f_count
),要么为此添加加号变量,它也将它转换为int(+$f_count
)
答案 1 :(得分:0)
只要与ChristopherW的回答合作,因为他是对的,你可以在这里看到差异。就像他说的那样,左侧的价值决定了运营商的待遇。
PS C:\Windows\system32> "5" * 5 # 5 is a string since it is in quotes
55555
PS C:\Windows\system32> [int]"5" * 5 # 5 is a number since it was cast as one.
25
PS C:\Windows\system32> $f_count.GetType().Fullname
System.String
最后,如果你看一下$f_count
的类型,你得到的答案是System.String
,这可以解释你所看到的内容。