为什么这个代码会跳过第3个“else if”?

时间:2013-08-27 16:27:27

标签: vbscript

以下代码有什么问题?它跳过第三个“否则如果”那个“你甚至睡觉吗?”

      Dim Message, i
  Dim strShutdown , lastword, strAbort

   i = Hour (Time)
   Message=InputBox("Hello and WHO might you be?","Virus")

if Message = " "  and i >= 8 and i < 12 then
WScript.Echo "Good Morning."
CreateObject("WScript.Shell").Run """C:\Documents and Settings\Admin\My           Documents\My Files\Designs\Vocaloid\Miku\Shimeji.jar"""
CreateObject("WScript.Shell").Run """C:\Documents and Settings\Admin\My Documents\My Files\Designs\Vocaloid\Miku\hello.vbs"""

Elseif Message = " " and i <= 17 and i >= 12 then
WScript.Echo "Good Afternoon."
CreateObject ("WScript.Shell").Run """C:\Documents and Settings\Admin\My Documents\My    Files\Designs\Vocaloid\Miku\Shimeji.jar"""
CreateObject ("WScript.Shell").Run """C:\Documents and Settings\Admin\My Documents\My Files\Designs\Vocaloid\Miku\hello.vbs"""

Elseif Message = " "  and i >= 18 and i < 20 then
WScript.Echo "Good Evening."
CreateObject ("WScript.Shell").Run """C:\Documents and Settings\Admin\My Documents\My Files\Designs\Vocaloid\Miku\Shimeji.jar"""
CreateObject ("WScript.Shell").Run """C:\Documents and Settings\Admin\My Documents\My Files\Designs\Vocaloid\Miku\hello.vbs"""

Elseif Message = " "  and i >= 20 and i < 8 then
WScript.Echo "Do you even sleep ?"
CreateObject ("WScript.Shell").Run """C:\Documents and Settings\Admin\My Documents\My Files\Designs\Vocaloid\Miku\Shimeji.jar"""
CreateObject ("WScript.Shell").Run """C:\Documents and Settings\Admin\My Documents\My Files\Designs\Vocaloid\Miku\hello.vbs"""

Else 
WScript.Echo "Hmm.... Intruder huh... heh!!."
strShutdown = "shutdown.exe -s -t 60 -f"
set objShell = CreateObject("WScript.Shell")
objShell.Run strShutdown, 0, false
WScript.Sleep 100

lastword= Inputbox("Computer will shutdown in 60 seconds. Any last words?" , "Virus")
If lastword = "stop it" then
WScript.Echo "Hmpf!"
strAbort = "shutdown.exe -a"
set objShell = CreateObject("WScript.Shell")
objShell.Run strAbort, 0, false
else
WScript.Echo "Sorry and Goodbye"
End if
End if

此外,我是否可以连接到MS Access,以便为其提供更多种类的回复和消息?如果您熟悉Desktop Buddies,我想创建一个并添加该部分,不仅可以在桌面上与其进行交互,还可以与它聊天。

2 个答案:

答案 0 :(得分:4)

我认为这是因为其他情况永远不会成真。我不能同时大于或等于20且小于8。也许你的意思是and i >= 20 OR i < 8 then?此外,我会将它们包含在parens中,以确保以正确的顺序计算逻辑,例如:

Elseif Message = " "  and (i >= 20 or i < 8) then

答案 1 :(得分:2)

在其他地方使用select case

为方便起见,我稍微重构了一下你的代码:

Dim Message
Dim lastword
Dim i
Dim strShutdown
Dim strAbort

strShutdown = "shutdown.exe -s -t 60 -f"
strAbort = "shutdown.exe -a"

i = Hour (Time)
Message = InputBox("Hello and WHO might you be?","Virus")

if Message = " " then
    Select Case i
        case 8, 9, 10, 11
            WScript.Echo "Good Morning."
        case 12, 13, 14, 15, 16, 17
            WScript.Echo "Good Afternoon."
        case 18, 19
            WScript.Echo "Good Evening."
        case else
            WScript.Echo "Do you even sleep ?"
    end select
    CreateObject ("WScript.Shell").Run """C:\Documents and Settings\Admin\My Documents\My Files\Designs\Vocaloid\Miku\Shimeji.jar"""
    CreateObject ("WScript.Shell").Run """C:\Documents and Settings\Admin\My Documents\My Files\Designs\Vocaloid\Miku\hello.vbs"""

Else 
    WScript.Echo "Hmm.... Intruder huh... heh!!."

    set objShell = CreateObject("WScript.Shell")
    objShell.Run strShutdown, 0, false
    WScript.Sleep 100

    lastword= Inputbox("Computer will shutdown in 60 seconds. Any last words?" , "Virus")
    If lastword = "stop it" then
        WScript.Echo "Hmpf!"
        set objShell = CreateObject("WScript.Shell")
        objShell.Run strAbort, 0, false
    else
        WScript.Echo "Sorry and Goodbye"
    End if
End if