使用多个 else if 语句

Jac*_*ack 5 vbscript

我试图用 VBScript 制作一个计算器来测试我的技能,但遇到了错误。我的程序使用多个 else if 语句来测试用户输入的内容。

下面是我的代码:

Dim head
Dim msg1, msgErr, msgAns
Dim input1, num1, num2
Dim ans

head = "Calculator"

msg1 = msgBox("Ruan's Vbscript calculator",0,head)
input1 = inputBox("How do you want to calculate? You can type (+ - * /)",head)

num1 = inputBox("Enter your first number",head)
num2 = inputBox("Enter your second number",head)

if (input1 = vbcancel) then
    wscript.quit()
else if (input1 = "+") then
    ans = num1 + num2
else if (input1 = "-") then
    ans = num1 - num2
else if (input1 = "*") then
    ans = num1 * num2
else if (input1 = "/") then
    ans = num1 / num2
else
    msgErr = msgBox("Make sure you type '+' or '-' or '*' or '/' with no extra letter or spaces","Error")
end if

msgAns = msgBox "Your answere is: " + head
Run Code Online (Sandbox Code Playgroud)

当我运行程序时,错误显示:“预期语句结束”。我没有看到这里的问题,正如我end if在所有这些else if陈述之后看到的那样。

Rah*_*thi 4

else删除和之间的空格if并使其成为elseif。像这样:

if (input1 = vbcancel) then
    wscript.quit()
elseif (input1 = "+") then
    ans = num1 + num2
elseif (input1 = "-") then
    ans = num1 - num2
elseif (input1 = "*") then
    ans = num1 * num2
elseif (input1 = "/") then
    ans = num1 / num2
else
    msgErr = msgBox("Make sure you type '+' or '-' or '*' or '/' with no extra letter or spaces","Error")
end if
Run Code Online (Sandbox Code Playgroud)

else通过和之间的额外空格,您可以开始嵌套在分支中的if新语句,而不是继续第一个语句。嵌套语句需要它们自己的 s,这就是您收到错误的原因。ifelseififend if