vb编程中 运行一下程序 会出现 “块IF没有END IF” 这是错在哪里?

来自:    更新日期:早些时候
各位楼主好请问我用vb编程时为什么运行程序时显示块if没有end if~

的确没有end if啊,还有 for 没有 next
我帮你改了下看看行不
'--------------------------------------------------
Private Sub form_load()
List1.AddItem "电子商务"
List1.AddItem "网页制作"
List1.AddItem "internet简明教程"
List1.AddItem "计算机网络基础"
List1.AddItem "多媒体技术"
End Sub
Private Sub Command1_Click()
List2.Clear

For i = O To List1.ListCount - 1

'--------------你这里用了if----------------
If List1.Selected(i) Then

List2.AddItem List1.List(i)

'-----------在这里加 END IF-------------
End If
'-----------------if语句结束---------------
Next i

End Sub
Private Sub command2_click()
List2.Clear
End Sub

If n Mod 2 0 Then MsgBox ("总脚数不是偶数")
End If
If n <= 2 * m Then MsgBox ("脚数小于头数的倍数,重新输入")
End If

这两条语句你直接把要执行的语句放到then的后面了,所以是不要 endif的。加了是多余的。会报错。


还有就是你的这个判断其实是个摆设,起不到实际作用。不管输入是否正确都会顺序执行下来。根据你的思路。你可以这样写:

Private Sub Form_click()
Dim m%, n%, x%, y%
m = Val(InputBox("请输入鸡和兔的总头数"))
j: n = Val(InputBox("请是输入鸡和兔的总脚数"))
If n Mod 2 0 Then
MsgBox ("总脚数不是偶数")
GoTo j
End If
If n <= 2 * m Then
MsgBox ("脚数小于头数的倍数,重新输入")
GoTo j
End If
y = (n - 2 * x) / 4
x = m - y
Print "鸡有:"; x; " 只 "; "兔有:"; y; "只"
End Sub

你的if ... endif用法好像有问题,里面不要加then参数,在写程序的时候尽量用缩进的方式,这样自己看着也明白,最简单的用法如下
if 表达式
命令语句集
else
命令语句集
endi
记住一点就是一个 if 要对着一个 endif ,少了就会出现嵌套错误!

第二段应该这样写:
Private Sub Command2_Click()
Dim prize As String, code As String
prize = Text1.Text
code = Text2.Text
If code = prize Then
Text3.Text = "恭喜你,中了一等奖1"
Elseif Left(code, 2) = Left(prize, 2) Then
Text3.Text = "恭喜你,中了二等奖!"
Elseif Left(code, 1) = Left(prize, 1) Then
Text3.Text = "恭喜你,中了三等奖!"
Else
Text3.Text = "谢谢你的参与!"
End If
End Sub

或者这样写:
Private Sub Command2_Click()
Dim prize As String, code As String
prize = Text1.Text
code = Text2.Text
If code = prize Then
Text3.Text = "恭喜你,中了一等奖1"
Else
If Left(code, 2) = Left(prize, 2) Then
Text3.Text = "恭喜你,中了二等奖!"
Else
If Left(code, 1) = Left(prize, 1) Then
Text3.Text = "恭喜你,中了三等奖!"
Else
Text3.Text = "谢谢你的参与!"
End If
End If
End If ''需要写三个End If
End Sub

Dim prize As String, code As String
prize = Text1.Text
code = Text2.Text
If code = prize Then
Text3.Text = "恭喜你,中了一等奖1"
ElseIf Left(code, 2) = Left(prize, 2) Then
Text3.Text = "恭喜你,中了二等奖!"
ElseIf Left(code, 1) = Left(prize, 1) Then
Text3.Text = "恭喜你,中了三等奖!"
Else
Text3.Text = "谢谢你的参与!"
End If

Option Explicit
Private Sub Command1_Click()
Dim i As Integer, s As Integer
Dim j As Single
Dim prize As String
For i = 1 To 3
For j = 0 To 1000 Step 0.0001
Next j
Randomize
s = Str(Int(10 * Rnd))
prize = prize & s
Text1.Text = prize
Text1.Refresh
Next i
End Sub

Private Sub Command2_Click()
Dim prize As String, code As String
prize = Text1.Text
code = Text2.Text
If code = prize Then
Text3.Text = "恭喜你,中了一等奖1"
Else
If Left(code, 2) = Left(prize, 2) Then
Text3.Text = "恭喜你,中了二等奖!"
Else
If Left(code, 1) = Left(prize, 1) Then
Text3.Text = "恭喜你,中了三等奖!"
Else
Text3.Text = "谢谢你的参与!"
End If
End If
End If
End Sub

少了几个EndIf

应该这样写
If code = prize Then
Text3.Text = "恭喜你,中了一等奖1"
ElseIf Left(code, 2) = Left(prize, 2) Then
Text3.Text = "恭喜你,中了二等奖!"
ElseIf Left(code, 1) = Left(prize, 1) Then
Text3.Text = "恭喜你,中了三等奖!"
Else
Text3.Text = "谢谢你的参与!"
End If


vb编程中 运行一下程序 会出现 “块IF没有END IF” 这是错在哪里?视频

相关评论:

相关主题精彩

版权声明:本网站为非赢利性站点,内容来自于网络投稿和网络,若有相关事宜,请联系管理员

Copyright © 喜物网