重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
‘这个是可以选择范围的查找
创新互联公司专注于文成企业网站建设,成都响应式网站建设公司,商城网站建设。文成网站建设公司,为文成等地区提供建站服务。全流程按需策划,专业设计,全程项目跟踪,创新互联公司专业和态度为您提供的服务
Public Class Form1
'一个大于1的自然数,除了1和它本身外,不能被整除以其他自然数(质数),换句话说就是该数除了1和它本身以外不再有其他的因数
' 章鱼哥 3107073263
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim PD As Boolean = True
TextBox1.Clear() '首先清空文本框,这里面将放置符合条件的素数
If NumericUpDown1.Value NumericUpDown2.Value Then '从小到大
For i = NumericUpDown1.Value To NumericUpDown2.Value '循坏判断所有数
For j = 2 To i - 1 '内循环是除了1和它本身的
If i Mod j = 0 Then '判断是否能被除了1和自身以为的 比自身小的数整除
PD = False '如果能被整除,将PD 设置为False
Exit For '退出里层For 循环
Else
PD = True
End If
Next
If PD Then '如果PD为True ,则为素数
TextBox1.Text = i " "
End If
Next
End If
End Sub
End Class
Module Module1
Sub Main()
Dim sum As Integer = 0
Dim Bound As Integer = 989
Dim num As Integer = 0
Dim cprime As Integer = 0
Do
num += 1
If IsPrame(num) Then
sum += num
cprime = num
End If
Loop Until sum Bound
Console.WriteLine(sum - cprime)
Console.ReadKey()
End Sub
'判断一个数是不是素数
Public Function IsPrame(ByVal num As Integer) As Boolean
If num = 1 Then Return False '素数的定义是大于1
For i = 2 To CType(Int(Math.Sqrt(num)), Integer)
If num Mod i = 0 Then Return False
Next
Return True
End Function
End Module
把你的程序改了一下,你是用数组来存放素数的,
For i=2 to 100这上循环是用来判断有哪些素数,并将其记录
而For J = 1 To UBound(Prime) 这个是用来输出找到的所有素数的
如果你用嵌套结构,每找一个数都将会输出一遍找到的所有的素数,当然会出错,只要将两个循环由嵌套结构变成顺序结构即可,如果一定要嵌套,你可以找到一个输出一个,不用数组,这样也不会重复而正确输出
这时,你还没有判断完,所以不能输出
Private Sub Form_Click()
Dim Prime() As Integer, I As Integer
Dim M As Integer, J As Integer
For I = 2 To 100
If sushu(I) Then
M = M + 1
ReDim Preserve Prime(M)
Prime(M) = I
End If
Next I
For J = 1 To UBound(Prime)
Print Prime(J);
If J Mod 10 = 0 Then Print
Next J
End Sub
Private Function sushu(N As Integer) As Boolean
Dim I As Integer
sushu = False
For I = 2 To Sqr(N)
If N Mod I = 0 Then Exit Function
Next I
sushu = True
End Function
Option Strict Off
Option Explicit On
Friend Class Form1
Inherits System.Windows.Forms.Form
Private Sub Command1_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command1.Click
Dim j As Integer
Dim A(9) As Short
Dim D(9) As Short
Dim i As Short
Dim B As Short
Dim C As Short
Dim F As Short
Dim G As Boolean
Dim H() As Short
Dim K As Short
Dim S As String
C = 32767
For i = 1 To 10
A(i - 1) = CShort(InputBox("输入第 " i "个数"))
If B A(i - 1) Then B = A(i - 1)
If C A(i - 1) Then C = A(i - 1)
D(i - 1) = Int(Rnd() * 100 + 0.5)
If F D(i - 1) Then F = D(i - 1)
Next i
For i = 2 To 100
For j = 2 To Int(System.Math.Sqrt(i) + 0.5)
'UPGRADE_WARNING: Couldn't resolve default property of object j. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
'UPGRADE_WARNING: Mod has a new behavior. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
If i Mod j 0 Then
G = True
Else
G = False
Exit For
End If
Next j
If G = True Then
ReDim Preserve H(K)
H(K) = i
K = K + 1
End If
Next i
S = "2;"
For i = 0 To K - 1
S = S H(i) ";"
Next i
MsgBox("最大数为:" B ";最小数为:" C)
MsgBox("最大的随机数为:" F)
MsgBox("1--100之间共有" K + 1 "个素数:" S)
End Sub
End Class
Private Sub Command1_Click()
Dim i As Integer
Dim sum As Integer
For i = 1 To 100
If Prime(i) Then
sum = sum + i
End If
Next
Print "100以内所有素数的和是" sum
End Sub
Private Function Prime(ByVal n As Integer) As Boolean
Dim i As Integer
If n 2 Then
Prime = False
Exit Function
End If
For i = 2 To Sqr(n)
If n Mod i = 0 Then
Prime = False
Exit Function
End If
Next
Prime = True
End Function
同意楼上的见解,
我在VB6.0中测试没有问题,能看到输出,VB.NET中也没有问题。
不过是否还有点得注意:
Print j 这句是把找到的素数以窗体的前景色(ForeColor)显示在窗体上,你指的没有输出,是指在窗体上看不到内容,是吧?是否你的窗体的前景色(ForeColor)与背景色(BackColor)相同或近似相同,造成视觉错误呢?