‘這個(gè)是可以選擇范圍的查找
創(chuàng)新互聯(lián)公司專注于文成企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,商城網(wǎng)站建設(shè)。文成網(wǎng)站建設(shè)公司,為文成等地區(qū)提供建站服務(wù)。全流程按需策劃,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)
Public?Class?Form1
'一個(gè)大于1的自然數(shù),除了1和它本身外,不能被整除以其他自然數(shù)(質(zhì)數(shù)),換句話說(shuō)就是該數(shù)除了1和它本身以外不再有其他的因數(shù)
'?章魚哥?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()?'首先清空文本框,這里面將放置符合條件的素?cái)?shù)
If?NumericUpDown1.Value??NumericUpDown2.Value?Then?'從小到大
For?i?=?NumericUpDown1.Value?To?NumericUpDown2.Value?'循壞判斷所有數(shù)
For?j?=?2?To?i?-?1?'內(nèi)循環(huán)是除了1和它本身的
If?i?Mod?j?=?0?Then?'判斷是否能被除了1和自身以為的?比自身小的數(shù)整除
PD?=?False?'如果能被整除,將PD?設(shè)置為False
Exit?For?'退出里層For?循環(huán)
Else
PD?=?True
End?If
Next
If?PD?Then?'如果PD為True?,則為素?cái)?shù)
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
'判斷一個(gè)數(shù)是不是素?cái)?shù)
Public?Function?IsPrame(ByVal?num?As?Integer)?As?Boolean
If?num?=?1?Then?Return?False?'素?cái)?shù)的定義是大于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
把你的程序改了一下,你是用數(shù)組來(lái)存放素?cái)?shù)的,
For i=2 to 100這上循環(huán)是用來(lái)判斷有哪些素?cái)?shù),并將其記錄
而For J = 1 To UBound(Prime) 這個(gè)是用來(lái)輸出找到的所有素?cái)?shù)的
如果你用嵌套結(jié)構(gòu),每找一個(gè)數(shù)都將會(huì)輸出一遍找到的所有的素?cái)?shù),當(dāng)然會(huì)出錯(cuò),只要將兩個(gè)循環(huán)由嵌套結(jié)構(gòu)變成順序結(jié)構(gòu)即可,如果一定要嵌套,你可以找到一個(gè)輸出一個(gè),不用數(shù)組,這樣也不會(huì)重復(fù)而正確輸出
這時(shí),你還沒有判斷完,所以不能輸出
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 "個(gè)數(shù)"))
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("最大數(shù)為:" B ";最小數(shù)為:" C)
MsgBox("最大的隨機(jī)數(shù)為:" F)
MsgBox("1--100之間共有" K + 1 "個(gè)素?cái)?shù):" 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以內(nèi)所有素?cái)?shù)的和是" 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中測(cè)試沒有問(wèn)題,能看到輸出,VB.NET中也沒有問(wèn)題。
不過(guò)是否還有點(diǎn)得注意:
Print j 這句是把找到的素?cái)?shù)以窗體的前景色(ForeColor)顯示在窗體上,你指的沒有輸出,是指在窗體上看不到內(nèi)容,是吧?是否你的窗體的前景色(ForeColor)與背景色(BackColor)相同或近似相同,造成視覺錯(cuò)誤呢?
網(wǎng)頁(yè)名稱:vb.net求素?cái)?shù),c語(yǔ)言求素?cái)?shù)的和
文章URL:http://vcdvsql.cn/article10/hshhdo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供、全網(wǎng)營(yíng)銷推廣、品牌網(wǎng)站建設(shè)、關(guān)鍵詞優(yōu)化、小程序開發(fā)、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容