【问题标题】:Variables being concatinated instead of added变量被连接而不是相加
【发布时间】:2016-09-11 12:17:12
【问题描述】:

为了测试我学到的东西,我正在用 VBScript 制作一个简单的计算器程序。 它为用户(我)提供了一个输入框来输入第一个数字,一个输入框用于运算符(+、-、* 或 /),第三个输入框用于输入第二个数字。三个运算符 -、* 和 / 工作得很好,但是当我测试加法时,程序将 num1 与 num2 连接而不是添加它,我不知道为什么。 这是我的代码:

dim num1,num2,answer,operators
num1 = inputbox("please enter your first number","calculator")
operators = inputbox("please enter either +,-,* or /","calculator")
num2 = inputbox("please enter your second number","calculator")
'keep the above lines, above everything else
Select Case operators
        Case "+"
        answer = (num1 + num2)
        Case "-"
        answer = (num1 - num2)
        Case "*"
        answer = (num1 * num2)
        Case "/"
        answer = (num1 / num2)
        case else
msgbox("please enter a valid operator")
End Select
msgbox answer

所以如果我说 6+7 的输出是 67。 我尝试将“= answer”放在末尾而不是开头,但它会引发“预期陈述”错误。

任何想法为什么它这样做?

【问题讨论】:

    标签: vbscript


    【解决方案1】:

    输入框返回一个字符串,所以“1”+“2”=“12”。试试:

    answer = (CInt(num1) + Cint(num2))
    

    更多类型转换函数列here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-06
      • 1970-01-01
      • 2020-03-03
      • 1970-01-01
      • 2022-12-22
      • 1970-01-01
      • 2011-08-23
      相关资源
      最近更新 更多