我需要制作一个VBSEditor程序,如果我是1 j必须是9,如果我是2 j必须是8,如果我是3 j必须是7等等

时间:2016-12-24 05:05:01

标签: vbscript

我需要制作一个VBSEditor程序,如果i为1,j必须为9,如果i为2,则j必须为8,如果{{ 1}}为3,i必须为7,依此类推。允许的j对的集合是:

i-j

到目前为止,我所做的是这样,但我遇到了诸如“(2,9)Microsoft VBScript编译错误:预期声明结束”等问题。

1-9, 2-8, 3-7, 4-6, 5-5, 6-4, 7-3, 8-2, 9-1

我需要的是一个公式或完整的代码,我会这样做。

1 个答案:

答案 0 :(得分:1)

请尝试以下代码之一:

i = 1
If i >= 1 And i <= 9 Then
    j = 10 - i
Else
    Err.Raise vbObjectError, , "Argument is out of range"
End If
MsgBox j

或任意值:

i = 1
Select Case i
    Case 1 j = 9
    Case 2 j = 8
    Case 3 j = 7
    Case 4 j = 6
    Case 5 j = 5
    Case 6 j = 4
    Case 7 j = 3
    Case 8 j = 2
    Case 9 j = 1
    Case Else Err.Raise vbObjectError, , "Argument is out of range"
End Select
MsgBox j

或包含在字符串中的大量值:

Data = "1-9, 2-8, 3-7, 4-6, 5-5, 6-4, 7-3, 8-2, 9-1"
Set Dict = CreateObject("Scripting.Dictionary")
For Each Item In Split(Data, ", ")
    Pair = Split(Item, "-")
    Dict(Clng(Pair(0))) = CLng(Pair(1))
Next

i = 1
If Dict.Exists(i) Then
    j = Dict(i)
Else
    Err.Raise vbObjectError, , "Argument is out of range"
End If
MsgBox j