题目:“用筛选法”找1--100之间的全部素数。对象窗口中有如下几个对象,一个图片控件,在图片控件上显示标签控件数组,程序运行时显示出筛选法的过程。另外两个控件是两个按纽对象,一个是“开始”,一个是“重置”。程序代码如下:
Private Sub Form_Load()
    Picture1.Move 0, 0
    Picture1.Height = (Label1(0).Height + 5) * 10 + 3
    Picture1.Width = (Label1(0).Width + 5) * 15 + 3
    Me.Height = Picture1.Height + 1900
    Me.Width = Picture1.Width + 800
    Label1(0).Visible = False
    
    For n = 1 To 100
       i = (n - 1) \ 10
       j = (n - 1) Mod 10
       
       Load Label1(n)
       
       With Label1(n)
        .Left = 5 + j * Label1(0).Width
        .Top = 5 + i * Label1(0).Height
        .Caption = n
        .Visible = ture
        .Caption = n
       End With
    Next
       
   
End Sub
Private Sub Command1_Click()
   Label1(1).Visible = False
   n = 100
   
   For i = 2 To Sqr(n)
    
    If Label1(i).Visible = ture Then
       MsgBox "现在开始删去" & Str(i) & "的倍数", 64, "用“筛选法”找素数"
      For j = i + 1 To n
         If Label1(j).Visible = ture And j Mod i = 0 Then Label1(j).Visible = False
      Next
    End If
    Next
    
    MsgBox "剩下来的整数已全部是素数!", 64, "用“筛选法”找素数"
           
End SubPrivate Sub Command2_Click()
   For n = 1 To 100
     Label1(n).Visible = True
   Next
End Sub调试问题:
   开始运行时不能显示1--100,点击重置后可以出现。
筛选的过程不能出现。请帮我看看什么原因?谢谢了!!

解决方案 »

  1.   

    这个……该是True不是ture……
      

  2.   

    所以……
    代码第一行加上 Option Explicit 很重要~
      

  3.   

    先谢谢楼上朋友!
    我不明白,能详细点吗?非常感谢!
    Option Explicit
      

  4.   

    功能:强制显式声明模块中的所有变量
    作用:可以避免在键入已有变量时出错加了这句后你再运行程序
    到了.Visible = ture
    就会报『变量未定义』
    就知道哪里错了……
    下面是MSDN的说明~=======================================================Option Explicit 语句在模块级别中使用,强制显式声明模块中的所有变量。语法Option Explicit说明如果使用,Option Explicit 语句必须写在模块的所有过程之前。如果模块中使用了 Option Explicit,则必须使用 Dim、Private、Public、ReDim 或 Static 语句来显式声明所有的变量。如果使用了未声明的变量名在编译时间会出现错误。如果没有使用 Option Explicit 语句,除非使用 Deftype 语句指定了缺省类型,否则所有未声明的变量都是 Variant 类型的。注意 使用 Option Explicit 可以避免在键入已有变量时出错,在变量的范围不是很清楚的代码中使用该语句可以避免混乱。
      

  5.   

    true 多处出错
    正常代码:(不全的你自己加上吧,不好意思)
    Private Sub Form_Load()
       
        Label1(0).Visible = False
        'Dim n, i, j As Integer
        For n = 1 To 100
           i = (n - 1) \ 10
           j = (n - 1) Mod 10
           
           Load Label1(n)
           Load Command3(n)
           With Label1(n)
            .Left = 5 + j * Label1(0).Width
            .Top = 5 + i * Label1(0).Height
            .Caption = n
            .Visible = True
            .Caption = n
           End With
        Next
           
       
    End Sub
    Private Sub Command1_Click()
       Label1(1).Visible = False
       n = 100
       
       For i = 2 To Sqr(n)
        
        If Label1(i).Visible = True Then
           MsgBox "现在开始删去" & Str(i) & "的倍数", 64, "用“筛选法”找素数"
          For j = i + 1 To n
             If Label1(j).Visible = True And j Mod i = 0 Then Label1(j).Visible = False
          Next
        End If
        Next
        
        MsgBox "剩下来的整数已全部是素数!", 64, "用“筛选法”找素数"
               
    End SubPrivate Sub Command2_Click()
       For n = 1 To 100
         Label1(n).Visible = True
       Next
    End Sub
      

  6.   

    Option Explicit在程序前加上这一句,果如你所说。还需要怎么怎么修改?再请教!我
      

  7.   

    晕……就是这样你再看看~
    Option ExplicitPrivate Sub Form_Load()
        Dim n As Integer
        Dim i As Integer, j As Integer
        
        Label1(0).Visible = False
        
        For n = 1 To 100
            i = (n - 1) \ 10
            j = (n - 1) Mod 10
            
            Load Label1(n)
            With Label1(n)
                .Move 5 + j * Label1(0).Width, 5 + i * Label1(0).Height
                .Caption = n
                .Visible = True
            End With
        Next
    End Sub
    Private Sub Command1_Click()
       Dim n As Integer
       Dim i As Integer, j As Integer
       Label1(1).Visible = False
       n = 100
       
       For i = 2 To Sqr(n)
        
        If Label1(i).Visible = True Then
            MsgBox "现在开始删去" & Str(i) & "的倍数", 64, "用“筛选法”找素数"
            For j = i + 1 To n
                If Label1(j).Visible = True And j Mod i = 0 Then Label1(j).Visible = False
                Next
            End If
        Next    MsgBox "剩下来的整数已全部是素数!", 64, "用“筛选法”找素数"
    End SubPrivate Sub Command2_Click()
        Dim n As Integer
        For n = 1 To 100
            Label1(n).Visible = True
        Next
    End Sub
      

  8.   

    把以下这段代码,替换你的代码即可!Private Sub Command1_Click()
       Label1(1).Visible = False
       n = 100
       For i = 2 To Sqr(n)
        If Label1(i).Visible = True Then
           MsgBox "现在开始删去" & Str(i) & "的倍数", 64, "用“筛选法”找素数"
          For j = i + 1 To n
             If Label1(j).Visible = True And j Mod i = 0 Then Label1(j).Visible = False
          Next
        End If
        Next
        
        MsgBox "剩下来的整数已全部是素数!", 64, "用“筛选法”找素数"
               
    End SubPrivate Sub Command2_Click()
       For n = 1 To 100
         Label1(n).Visible = True
         Next
    End SubPrivate Sub Form_Load()
        Picture1.Move 0, 0
        Picture1.Height = (Label1(0).Height + 5) * 10 + 3
        Picture1.Width = (Label1(0).Width + 5) * 15 + 3
        Me.Height = Picture1.Height + 1900
        Me.Width = Picture1.Width + 800
        
        Label1(0).Visible = False
        
        For n = 1 To 100
           i = (n - 1) \ 10
           j = (n - 1) Mod 10
           Load Label1(n)
           With Label1(n)
            .Left = 5 + j * Label1(0).Width
            .Top = 5 + i * Label1(0).Height
            .Visible = True
            .Caption = n
        End With
       Next
       
    End Sub