2015-06-01 43 views
4

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

这里是我下面的代码:

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 

当我运行该程序,错误说:“预期语句的结束”。我没有看到这里的问题,因为我有end if所有这些else if声明。

+0

我相信VBScript中的正确的语法是'ElseIf' http://www.w3schools.com/vbscript/vbscript_conditionals.asp –

+0

'输入1 = vbcancel'不会因为有些人可能认为工作。 –

+2

任何愿意解释为什么一个计算器声称5 + 7是57的计算器值得分吗? –

回答

3

删除elseif之间的空格,并将其设置为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 

随着else之间的额外空间if你开始一个新的if声明嵌套在else分支,而不是继续第一if声明。嵌套的if声明需要自己的end if,这就是为什么你会收到错误。

+0

'input1 = vbcancel'不像某些人想象的那样工作。 –

0

一个Select Case通常是基于单个变量更简单/更清晰的选择操作施工产值

Select Case input1 
    Case vbCancel 
     wscript.quit() 
    Case "+" 
     ans = num1 + num2 
    Case "-" 
     ans = num1 - num2 
    Case "*" 
     ans = num1 * num2 
    Case "/", "\" '//multiples 
     ans = num1/num2 
    Case Else 
     msgBox "Make sure you type '+' or '-' or '*' or '/' with no extra letter or spaces", , "Error" 
End Select 
+0

请重新考虑'Case“/”,“\”'// multiples'(误导性评论,真实与整数div)。 –

+1

'input1 = vbcancel'不像某些人想象的那样工作。 –

2

一些小改进,使其工作:

Option Explicit 

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

head = "Calculator" 
msgBox "Ruan's Vbscript calculator", 0, head 
input1 = inputBox("How do you want to calculate? You can type (+ - * /)",head) 
If Not IsEmpty(input1) Then 
    num1 = CDbl(inputBox("Enter your first number",head)) 
    num2 = CDbl(inputBox("Enter your second number",head)) 
    if  input1 = "+" then 
     ans = num1 + num2 
    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 
    msgBox "Your answere is: " & ans 
else 
    msgBox "Aborted" 
end if 

类型和范围将检查operants犹豫不决。