方法一:使用嵌套循环
```vb
Private Sub Command1_Click()
Dim i As Integer
Dim j As Integer
Dim yn As Boolean
For i = 1 To 100
yn = True
For j = 2 To i - 1
If i Mod j = 0 Then
yn = False
Exit For
End If
Next j
If yn = True Then
Print i
End If
Next i
End Sub
```
方法二:使用平方根优化
```vb
Private Sub Command1_Click()
Dim i As Long
Dim j As Long
Dim k As Long
For i = 1 To 100
k = Int(Sqr(i))
For j = 2 To k
If i Mod j = 0 Then
Exit For
End If
Next j
If j > k Then
Print i
End If
Next i
End Sub
```
方法三:使用筛选法
```vb
Private Sub Form_Click()
Const N = 100
Dim i As Integer
Dim j As Integer
Dim Line As Integer
Dim a(N) As Integer
For i = 0 To N - 1
a(i) = i
Next i
For i = 2 To Sqr(N)
For j = i + 1 To N
If a(i) = 0 And a(j) = 0 Then
If a(j) Mod a(i) = 0 Then
a(j) = 0
End If
End If
Next j
Next i
Print "显示出100以内的素数"
For i = 2 To N - 1
If a(i) = 0 Then
Print a(i); " "
Line = Line + 1
End If
If Line Mod 10 = 0 Then
End If
Next i
End Sub
```
方法四:使用函数判断
```vb
Private Sub Command1_Click()
Dim n As Integer
For n = 1 To 100
If IsPrime(n) Then
List1.AddItem n
End If
Next n
End Sub
Public Function IsPrime(x As Integer) As Boolean
Dim i As Integer
For i = 2 To x - 1
If x Mod i = 0 Then
IsPrime = False
Exit For
End If
Next i
IsPrime = True
End Function
```
方法五:使用控制台输出
```vb
Module Program
Sub Main(args As String())
Dim startNum As Integer = 2 ' 起始数字
Dim endNum As Integer = 100 ' 结束数字
Console.WriteLine("在范围 " & startNum & " 到" & endNum & " 内的素数有:")
For num As Integer = startNum To endNum
Dim isPrime As Boolean = True ' 默认当前数字是素数
For i As Integer = 2 To Math.Sqrt(num)
If num Mod i = 0 Then ' 如果能被2到平方根之间的数整除,则不是素数
isPrime = False
Exit For
End If
Next
If isPrime Then
Console.WriteLine(num)
End If
Next
Console.ReadLine()
End Sub
End Module
```
这些方法都可以有效地找出1到100之间的所有质数,并输出到窗体或控制台。你可以根据自己的需求和编程习惯选择合适的方法。