vb实现随机生成100个整数,将其中的奇数由大到小排序,偶数由小到大排序,找出其中所有素数并求和

vb实现随机生成100个整数,将其中的奇数由大到小排序,偶数由小到大排序,找出其中所有素数并求和

  • 一.程序运行界面
  • 二. 代码部分
    • 1.全局变量的声明
    • 2. 窗体内的代码
      • 1>生成100个随机数
      • 2> 分开数组中的奇偶数分别放在两个数组里,然后对奇数数组排序
      • 3>偶数数组排序
      • 4>找出数组中所有的素数
      • 5>计算素数个数及总合

一.程序运行界面

vb实现随机生成100个整数,将其中的奇数由大到小排序,偶数由小到大排序,找出其中所有素数并求和_第1张图片vb实现随机生成100个整数,将其中的奇数由大到小排序,偶数由小到大排序,找出其中所有素数并求和_第2张图片

二. 代码部分

1.全局变量的声明

 
    Public a(100) As Integer 
    Public oA(100) As Integer 
    Public jA(100) As Integer 
    Public x As Integer 
    Public y As Integer 
    Public sA(100) As Integer
    Public z As Integer 
    

vb实现随机生成100个整数,将其中的奇数由大到小排序,偶数由小到大排序,找出其中所有素数并求和_第3张图片

2. 窗体内的代码

1>生成100个随机数


Private Sub Command1_Click() 
    Dim n As Integer
    Dim str As String

    For i = 0 To 99
       a(i) = Int(Rnd() * 100 + 1)
    Next

    For i = 1 To 100
        If i Mod 10 = 0 Then
            str = str & a(i - 1) & vbCrLf
        Else
            str = str & a(i - 1) & vbTab
        End If
    Next
    Text1.Text = str

    x = 0:y = 0:z = 0
    Text2.Text = vbNullString
    Text3.Text = vbNullString
    Text4.Text = vbNullString
    Text5.Text = vbNullString
    Text6.Text = vbNullString
End Sub

2> 分开数组中的奇偶数分别放在两个数组里,然后对奇数数组排序

Private Sub Command2_Click()

    For i = 1 To 100
        If a(i - 1) Mod 2 = 0 Then
            oA(x) = a(i - 1)
            x = x + 1
        Else
            jA(y) = a(i - 1)
            y = y + 1
        End If
    Next

    Dim t As Integer
    For i = 0 To y - 1
        For j = i + 1 To y - 1
            If jA(i) > jA(j) Then
                t = jA(j)
                jA(j) = jA(i)
                jA(i) = t
            End If
        Next
    Next
    For i = 1 To y
        If i Mod 6 = 0 Then
            Text2.Text = Text2.Text & jA(i - 1) & vbCrLf
        Else
            Text2.Text = Text2.Text & jA(i - 1) & vbTab
        End If
    Next
End Sub

3>偶数数组排序

Private Sub Command3_Click()
    Dim t As Integer
    For i = 0 To x - 1
        For j = i + 1 To x - 1
            If oA(i) < oA(j) Then
                t = oA(j)
                oA(j) = oA(i)
                oA(i) = t
            End If
        Next
    Next
    For i = 1 To x
        If i Mod 6 = 0 Then
            Text3.Text = Text3.Text & oA(i - 1) & vbCrLf
        Else
            Text3.Text = Text3.Text & oA(i - 1) & vbTab
        End If
    Next
End Sub

4>找出数组中所有的素数


Private Sub Command4_Click()

    Dim boo As Boolean
    For i = 0 To 99
        boo = True
        
        If a(i) < 3 Then
            If a(i) = 0 Then
                boo = False
            ElseIf a(i) = 1 Then
                boo = True
            ElseIf a(i) = 2 Then
                boo = False
            End If
        Else
            For j = 2 To a(i) - 1
                If a(i) Mod j = 0 Then
                    boo = False
                    Exit For
                End If
            Next
        End If
            
        If boo = True Then
            sA(z) = a(i)
            z = z + 1
        End If
    Next
    For i = 1 To z
        If i Mod 6 = 0 Then
            Text4.Text = Text4.Text & sA(i - 1) & vbCrLf
        Else
            Text4.Text = Text4.Text & sA(i - 1) & vbTab
        End If
    Next
    
End Sub

5>计算素数个数及总合


Private Sub Command5_Click()    
    Dim s As Integer
    For i = 0 To z - 1
        s = s + sA(i)
    Next
    Text5.Text = s
    Text6.Text = z
End Sub

你可能感兴趣的:(VB,实例,源码,网络编程)