‘這個(gè)是可以選擇范圍的查找
10余年的興和網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。營銷型網(wǎng)站的優(yōu)勢(shì)是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整興和建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)建站從事“興和網(wǎng)站設(shè)計(jì)”,“興和網(wǎng)站推廣”以來,每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
Public?Class?Form1
'一個(gè)大于1的自然數(shù),除了1和它本身外,不能被整除以其他自然數(shù)(質(zhì)數(shù)),換句話說就是該數(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
同意樓上的見解,
我在VB6.0中測(cè)試沒有問題,能看到輸出,VB.NET中也沒有問題。
不過是否還有點(diǎn)得注意:
Print j 這句是把找到的素?cái)?shù)以窗體的前景色(ForeColor)顯示在窗體上,你指的沒有輸出,是指在窗體上看不到內(nèi)容,是吧?是否你的窗體的前景色(ForeColor)與背景色(BackColor)相同或近似相同,造成視覺錯(cuò)誤呢?
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
vb判斷素?cái)?shù)需要添加的控件有button和listbox。
1、打開vb工程,在上面建一個(gè)窗體,然后創(chuàng)建button和listbox
2、在findPrime上寫入如下代碼:
Private Sub CommandButton1_Click()
Dim flag As Boolean
flag = True
ListBox1.AddItem (1) '默認(rèn)1和2都是素?cái)?shù)
ListBox1.AddItem (2)
For i = 1 To 1000
For J = 2 To i - 1
If (i Mod J = 0) Then
flag = True
Exit For
Else
flag = False
End If
Next
If flag = False Then
ListBox1.AddItem (i)
End If
Next i
End Sub
3、運(yùn)行結(jié)果,列出了1-1000內(nèi)的所有素?cái)?shù)。
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