Windows Powershell-将两个变量添加到一起,从命令行接收数字
我试图从命令行接收输入,例如添加两个数字,但我要么返回文本变量number、数字0,要么返回无效数据 使用此代码:Windows Powershell-将两个变量添加到一起,从命令行接收数字,windows,powershell,Windows,Powershell,我试图从命令行接收输入,例如添加两个数字,但我要么返回文本变量number、数字0,要么返回无效数据 使用此代码: Param ( [int]$a, [int]$b ) function add([int]$a,[int]$b) { $var = $a+"$b" $a $b $var return $a + $b } add([int]$a,[int]$b) Param ( [int]$a, [int]$b ) fu
Param (
[int]$a,
[int]$b
)
function add([int]$a,[int]$b)
{
$var = $a+"$b"
$a
$b
$var
return $a + $b
}
add([int]$a,[int]$b)
Param (
[int]$a,
[int]$b
)
function add($a,$b)
{
$var = $a+"$b"
$a
$b
$var
return $a + $b
}
add($a,$b)
这就是我收到的错误
PS C:\> .\add.ps1 -a 5 -b 4
add : Cannot process argument transformation on parameter 'a'. Cannot convert the "System.Object[]" value of type "System.Object[]" to type "System.Int32".
At C:\add.ps1:15 char:4
+ add <<<< ([int]$a,[int]$b)
+ CategoryInfo : InvalidData: (:) [add], ParameterBindin...mationException
+ FullyQualifiedErrorId : ParameterArgumentTransformationError,add
它只返回0。然后,当我使用此代码时:
Param (
[int]$a,
[int]$b
)
function add([int]$a,[int]$b)
{
$var = $a+"$b"
$a
$b
$var
return $a + $b
}
add([int]$a,[int]$b)
Param (
[int]$a,
[int]$b
)
function add($a,$b)
{
$var = $a+"$b"
$a
$b
$var
return $a + $b
}
add($a,$b)
它只是返回我作为命令行参数给出的内容。我这样称呼这个程序:
\add.ps1-a5-b4
我在Powershell上肯定做得不对。但是,我不确定搜索应该使用什么术语,或者如何命名,因为我知道如何在函数中串联变量,或者只添加普通整数,而不是两个从命令行传递整数的变量 您可能不想重复使用变量名,因为这会让人困惑。我将函数中的变量改为
$c
和$d
。然后像调用脚本一样调用函数,它工作得很好
Param (
[int]$a,
[int]$b
)
function add([int]$c,[int]$d)
{
$var = $c+$d
$c
$d
$var
return $d + $c
}
add -c $a -d $b
PS C:\test> .\add.ps1 -a 5 -b 4
5
4
9
9
您可能不想重复使用变量名,因为这会让人困惑。我将函数中的变量改为
$c
和$d
。然后像调用脚本一样调用函数,它工作得很好
Param (
[int]$a,
[int]$b
)
function add([int]$c,[int]$d)
{
$var = $c+$d
$c
$d
$var
return $d + $c
}
add -c $a -d $b
PS C:\test> .\add.ps1 -a 5 -b 4
5
4
9
9
函数(argument1,argument2)
不是调用powershell函数的正确语法。调用不带参数名的PowerShell函数的正确语法是函数argument1 argument2
你在干什么
add([int]$a,[int]$b)
正在为add函数提供单个数组参数,并且该数组是无法转换为错误消息中提到的Int32的对象。换句话说,您提供数组([int]$a,[int]$b)
作为add函数的$a参数的值,而不是为add函数的$b参数提供值*
此外,PowerShell是一种动态类型化语言;无需在函数调用中将参数$a和$b强制转换为[int],特别是因为您已经在参数列表中强制转换了它们。将最后一行更改为:
add $a $b
如果出于某种原因确实希望将参数强制转换为函数调用,可以通过将每个强制转换作为表达式独立计算来实现:
add ([int]$a) ([int]$b)
*为了说明这一点,请将添加功能更改为:
function add($a, $b)
{
"`nValue of parameter A:"
$a
"`nType of parameter A:"
$a.GetType()
"`nValue of parameter B:"
$b
"`nType of parameter B:"
$b.GetType()
}
当你把它叫做
add([int]$a,[int]$b)
得到以下结果,表明A是两个整数的数组,而B未定义:
Value of parameter A:
4
5
Type of parameter A:
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
Value of parameter B:
Type of parameter B:
You cannot call a method on a null-valued expression.
At C:\Data Files\scratch\so\soscratch\add.ps1:15 char:3
+ $b.GetType()
+ ~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
函数(argument1,argument2)
不是调用powershell函数的正确语法。调用不带参数名的PowerShell函数的正确语法是函数argument1 argument2
你在干什么
add([int]$a,[int]$b)
正在为add函数提供单个数组参数,并且该数组是无法转换为错误消息中提到的Int32的对象。换句话说,您提供数组([int]$a,[int]$b)
作为add函数的$a参数的值,而不是为add函数的$b参数提供值*
此外,PowerShell是一种动态类型化语言;无需在函数调用中将参数$a和$b强制转换为[int],特别是因为您已经在参数列表中强制转换了它们。将最后一行更改为:
add $a $b
如果出于某种原因确实希望将参数强制转换为函数调用,可以通过将每个强制转换作为表达式独立计算来实现:
add ([int]$a) ([int]$b)
*为了说明这一点,请将添加功能更改为:
function add($a, $b)
{
"`nValue of parameter A:"
$a
"`nType of parameter A:"
$a.GetType()
"`nValue of parameter B:"
$b
"`nType of parameter B:"
$b.GetType()
}
当你把它叫做
add([int]$a,[int]$b)
得到以下结果,表明A是两个整数的数组,而B未定义:
Value of parameter A:
4
5
Type of parameter A:
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
Value of parameter B:
Type of parameter B:
You cannot call a method on a null-valued expression.
At C:\Data Files\scratch\so\soscratch\add.ps1:15 char:3
+ $b.GetType()
+ ~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
来自Adi技术人员和Adi Inbar的这两个答案都是正确的。谢谢你们。来自MadTechnician和Adi Inbar的这两个答案都是正确的。谢谢你们。