回 帖 发 新 帖 刷新版面

主题:初学者请教个问题

为什么我只显示1个数的呢?请问哪里错了?

 

Dim y As String, i As Integer
Private Sub Command1_Click(Index As Integer)
If Index = 0 Then
Timer1(0).Enabled = True
y = 0
ElseIf Index = 2 Then
End
End If
End Sub

Private Sub Timer1_Timer(Index As Integer)
For i = 0 To 5
y = y + i
If Index = y Then
Label2(i) = Int(Rnd * 10)
Timer1(y).Enabled = False
Timer1(y + 1).Enabled = True
End If
Next i
End Sub

 

回复列表 (共3个回复)

沙发

去掉 If Index = y Then 和后面的 end if 。

不过,一个小小的程序动用了6个计时器,有点浪费。建议只留一个计时器,代码这样改一下:


Dim i As Integer

Private Sub Command1_Click(Index As Integer)
Select Case Index
  Case 0: Timer1.Enabled = True
  Case 1: For i = 0 To 5: Label2(i) = "": Next
  Case 2: End
End Select
End Sub

Private Sub Timer1_Timer()
For i = 0 To 5
  Label2(i) = Int(Rnd * 10)
Next i
Timer1.Enabled = False
End Sub

板凳

[quote]
For i = 0 To 5
  y = y + i
[color=FF0000]

    If Index = y Then
[/color]
       Label2(i) = Int(Rnd * 10)
       Timer1(y).Enabled = False
       Timer1(y + 1).Enabled = True
    End If
Next i
[/quote]
有逻辑错误!

看看分析:

1、点击“开始”命令按钮,计时器(0)开始启动,y赋值为0;

2、计时器(0)的Timer事件作用。看看这段代码执行过程:

  1)for第一次开始,y=0,if条件为真,运行then……end if代码,

     此时,Label2(0)进行了赋值,计时器(0)停止(但它的第一个Timer事件还没结束,还要进行for),计时器(1)开始工作;


  2)接下来运行第二次for语句,y = y + i语句执行后,y=1,但Index值未变,

    if条件为假,所以if ……end if中间的语句不执行,

    但是计时器(0)的第一次Timer事件还没结束,程序继续运行


  3)又进行第三次for循环,如第二次循环一样,

    一直要到i=5时,y = y + i语句执行后,y=1+2+3+4+5=15,

    换句话说,计时器(0)的第一次Timer事件完成后,y=15;

3、接下来才是计时器(1)的Timer事件作用,再看代码:

   在计时器(1)的任何次Timer事件中,if的条件语句值肯定为假,故if……end if并不执行。

  综上所述,你的程序最终运行的结果:只有Label2(0)赋值,Label2(x)都没有赋值

3 楼

Major League Baseball "45" number. So then [url=http://www.cheapjordanforsale.us/air-jordan-4-retro-c-4.html]retro jordan 4[/url] shoes followed by No. 45 embroidered white line no.3, 5000 dollar fines dispute In the second game of the Eastern Conference [url=http://www.cheapjordanforsale.us/air-jordan-6-retro-c-6.html]retro jordan 6[/url] semifinals, the Father re-donned the jersey number "23", scored 38 points, but the foot wear or [url=http://www.cheapjordanforsale.us/air-jordan-7-retro-c-8.html]retro jordan 7[/url] embroidered with "45" was too black and white [url=http://www.cheapjordanforsale.us/air-jordan-9-retro-c-9.html]retro jordan 9[/url], then the Bulls away combat, black and white home color [url=http://www.cheapjordanforsale.us/air-jordan-10-retro-c-10.html]retro jordan 10[/url] and away the color does not match, so the Father was also fined $ 5,000 per game, the Father suddenly re-wear jersey No. 23, again by a [url=http://www.cheapjordanforsale.us/air-jordan-11-retro-c-11.html]retro jordan 11[/url] fine of $ 25,000 per game. concord directly translated into "Concord grapes", varieties of Americas grapes, [url=http://www.cheapjordanforsale.us/air-jordan-11-retro-c-11.html]jordan 11 Concord[/url] crystal outsole echoes the color with purple soles, while the color name from the soles of purple the no.5, hot on sale very terrible [url=http://www.cheapjordanforsale.us/air-jordan-12-retro-c-12.html]retro jordan 12[/url] finally listed, each Offer shops outside there are people waiting in line, some people even crushed glass [url=http://www.cheapjordanforsale.us/air-jordan-13-retro-c-13.html]retro jordan 13[/url] doors across the security downtown into the store to try to steal these shoes, in order to buy it at all costs, its all crazy!

我来回复

您尚未登录,请登录后再回复。点此登录或注册