2009年1月6日 星期二

陣列應用基礎題

image

Public Class Form1
    Dim i, s
    Dim str1 = ""
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim a() = {2, 1, 4, 3, 6, 5, 8, 7, 10, 9}

        '合計
        For i = 0 To 9
            s = s + a(i)
        Next
        str1 = str1 & "合計:" & s & vbNewLine

        '求最大值
        Dim maxN = a(0)
        For i = 1 To 9
            If a(i) > maxN Then maxN = a(i)
        Next
        str1 = str1 & "最大值:" & maxN & vbNewLine

        '求最小值
        Dim minN = a(0)
        For i = 1 To 9
            If a(i) < minN Then minN = a(i)
        Next
        str1 = str1 & "最小值:" & minN & vbNewLine

        '求平均
        str1 = str1 & "平均:" & s / 10 & vbNewLine

        '求2的倍數有多少個
        Dim c2 = 0
        For i = 0 To 9
            If a(i) Mod 2 = 0 Then
                c2 = c2 + 1
            End If
        Next
        str1 = str1 & "2的倍數共有:" & c2 & "個" & vbNewLine

        '求3的倍數有多少個
        Dim c3 = 0
        For i = 0 To 9
            If a(i) Mod 3 = 0 Then
                c3 = c3 + 1
            End If
        Next
        str1 = str1 & "3的倍數共有:" & c3 & "個" & vbNewLine

        '求又是2又是3的倍數有多少個
        Dim c4 = 0
        For i = 0 To 9
            If a(i) Mod 2 = 0 And a(i) Mod 3 = 0 Then
                c4 = c4 + 1
            End If
        Next
        str1 = str1 & "又是2又是3的倍數有:" & c4 & "個" & vbNewLine

        '求是2或者是3的倍數有多少個
        Dim c5 = 0
        For i = 0 To 9
            If a(i) Mod 2 = 0 Or a(i) Mod 3 = 0 Then
                c5 = c5 + 1
            End If
        Next
        str1 = str1 & "是2或者是3的倍數有:" & c5 & "個" & vbNewLine
        MsgBox(str1)
    End Sub
End Class

沒有留言:

張貼留言