在vbscript中的不同情况下使用+运算符

mul*_*lla 1 vbscript qtp adsutil.vbs

vbscript中以下的值是什么?

1)x=1+"1" 2)x="1"+"1" 3)x=1+"mulla" 注意:在以上三种情况中,我使用第一个变量作为字符串或整数,第二个作为字符串使用.

情况1:在操作期间作为数字和自动转换为数字

enter code here
y=inputbox("Enter a numeric value","") Rem I am using 1 as input
x=1
msgbox x+y Rem value is 2
msgbox x*y Rem value is 1
Run Code Online (Sandbox Code Playgroud)

情况2:作为一个字符串,在操作期间没有转换为数字,它失败了

enter code here
y=inputbox("Enter a numeric value","") Rem I am using 1 as input
x=1
if y= x then
    msgbox "pass"
else
    msgbox "fail"
end if
Run Code Online (Sandbox Code Playgroud)

情况3:在操作过程中作为字符串和显式转换为数字

enter code here
y=inputbox("Enter a numeric value","") Rem I am using 1 as input
x=1
if Cint(y) = x then
    msgbox "pass"
else
    msgbox "fail"
end if
Run Code Online (Sandbox Code Playgroud)

我需要一个不同行为的逻辑原因.但在其他语言中,它是直截了当的,并将按预期工作

小智 5

参考: 加法运算符(+)(VBScript)

虽然您也可以使用+运算符来连接两个字符串,但您应该使用&运算符来连接以消除歧义.使用+运算符时,您可能无法确定是否会发生加法或字符串连接.表达式的类型以下列方式确定+运算符的行为:

如果Both表达式都是数字,则结果是两个数字相加.

如果Both表达式都是字符串,则结果是两个字符串的串联.

如果一个表达式是数字而另一个是字符串,那么Error: type mismatch将抛出一个表达式.

使用混合数据类型时,最好使用类型转换函数将变量转换为通用数据类型.