我的VBS代码不起作用

时间:2017-05-18 08:58:33

标签: vbscript

我试图在VBScript中进行一些编码而且我遇到了错误,但我不知道出了什么问题。

a = InputBox("Enter Numerator")
b = InputBox("Enter Denominator")

If b = 0 Then
  MsgBox "b is 0 , that's not right probably, I'll set  b=1"
  b = 1

MgBox "Contribution" & a & ":" & b & "=" & MojeDeleni(a, b)

Function MySplitting(a,b)
  On Error Resume Next
  MySplitting = a/b
  If Err.Number <> 0 Then
    MySplitting = "Splitting error no." & CStr(Err.Number) & " " & Err.Description
  End if
  Err.Clear
End Function

1 个答案:

答案 0 :(得分:0)

您的代码中存在一些拼写错误。

If B = "" Then '<< String CANNOT BE Integer, YOU SHOULD USE "" TO CHECK IF IT IS EMPTY >>
  MsgBox "Denominator is 0, That's not right probably, I'll set Denominator to 1"
  B = 1
End If '<< End If IS MISSING HERE >>

或者您可以使用If CInt(B) = 0 Then

Function MySplitting(A, B)
   On Error Resume Next
   MySplitting = CInt(A) / CInt(B)
   If Err.Number <> 0 Then
      MySplitting = "Splitting Error : " & CStr(Err.Number) & " " & 
Err.Description
   End If
   Err.Clear
End Function

MgBox "Contribution" & A & " : " & B & " = " & MojeDeleni(A, B) '<< HERE MgBox SHOULD BE MsgBox >>

如果您使用一种语言分配了一个新变量并希望将其与另一种语言一起使用,则除非您告诉脚本引擎它们是如下所示的相同变量,否则无法执行此操作。

MojeDeleni = MySplitting(A, B)

所以现在应该工作。

MsgBox "Contribution is " & A & " : " & B & " = " & MojeDeleni

为防止在发生任何错误时显示“贡献”消息,您可以执行以下操作:

MojeDeleni = MySplitting(A, B)

If InStr(Result, "Error") = 0 Then
  MsgBox "Contribution is " & A & " : " & B & " = " & MojeDeleni
Else
  MsgBox MojeDeleni
End If