0

如何在 Visual Basic 6if中使用另一个 ?if

例如,在 C++ 中,我可以用 开始我的语句{并用 结束它们,并且我可以在第一条语句}中使用另一个。if

例如:

if m>3
{       .....
     if a>5
      {
       ......
      }
     else {....}
} else ........

…但在 Visual Basic 中没有{ }或类似的东西

我想编写一个程序,从用户那里接收时间并从中减少 4:30!例如,当用户运行它时,它会问:在德黑兰几点钟了?和用户输入时间(例如 16:30)然后程序从它减少 4 小时 30 分钟!并打印:格林威治标准时间是:12:00!我会更新我的帖子并输入我的代码!请阅读!

Option Explicit

Private Sub Command1_Click()
    Cls
    Dim h As Integer, m As Integer, hm As Integer, mm As Integer, t As Integer
    Dim fh As String, fm As String, fhm As String, fmm As String

    t = InputBox("Enter time")
    h = t \ 100
    m = t Mod 100

    Select Case h

    Case 0 To 24

        If m < 60 Then
            If m >= 30 Then

                hm = h - 4
                mm = m - 30

                If h < 10 Then fh = Format(h, String(2, "0")) Else fh = h
                If m < 10 Then fm = Format(m, String(2, "0")) Else fm = m
                If hm < 10 Then fhm = Format(hm, String(2, "0")) Else fhm = hm
                If mm < 10 Then fmm = Format(mm, String(2, "0")) Else fmm = mm

                Print
                Print "      Tehran  Time is  :   "; fh; ":"; fm
                Print
                Print "      GMT       Time is  :   "; fhm; ":"; 
            Else
                hm = h - 5
                mm = m + 30

                If h < 10 Then fh = Format(h, String(2, "0")) Else fh = h
                If m < 10 Then fm = Format(m, String(2, "0")) Else fm = m
                If hm < 10 Then fhm = Format(hm, String(2, "0")) Else fhm = hm
                If mm < 10 Then fmm = Format(mm, String(2, "0")) Else fmm = mm

                Print
                Print "      Tehran  Time is  :   "; fh; ":"; fm
                Print
                Print "      GMT       Time is  :   "; fhm; ":"; fmm
            End If
        End If

    Case Else

        Beep
        Print
        Print "   Time is wrong !!!  Try again "

    End Select

End Sub

Private Sub Command2_Click()
        End
End Sub

Private Sub Form_Load()

End Sub
4

2 回答 2

13

VB 中的 If 子句以“End If”结尾(不区分大小写)。没有任何括号。

你可以嵌套任意数量的 if 子句,只要你关闭它们:

If ... Then
    *Do stuff*

    If ... Then
        *Do more stuff*
    End If
End If
于 2013-03-29T18:05:27.103 回答
7

单行 If 语句也可以嵌套:

If (a) Then [a_stuff] : If (b) Then [a_and_b_stuff] Else [a_and_notb_stuff] Else [nota_stuff]

请注意,[a_stuff]后面不能再出现[a_and_notb_stuff],因为无法判断 b 的 Else 语句在哪里结束,因此嵌套 If 语句之后的子句中不会出现任何内容。

任何*_stuff部分都可以是空的。但是你可能对空[a_and_notb_stuff]的 . 要解决此问题,您可以使用 ' :' 代替[a_and_notb_stuff]. IDE 将删除冒号,但不会插入另一个 Else 语句。

Addendum: I should point out that this whole post should be treated as information, and not as a recommendation. Nesting single-line If statements like this makes for very unreadable and bug-prone code.

于 2013-03-29T19:08:54.700 回答