1

我有这门课,有很多收藏

    'class properties
     public a as collection
     public b as collection
     public c as collection

a,b,c 包含诸如“a,a,a,a,b,b,b,b,c,c,c,c,c,c,c”之类的值,b
和 c 也是如此,而且更有可能. 这些值可以是我们可以假设的任何字母。为了获得每个类的每个属性的百分比,我想这样做
(骨架,因为我不知道该怎么做)

     for each class in collectionOfclassobjects
       for each ca in class.a
       'here am supposed to count all a's and b's divide by class.a.count but am not sure how to do this, I have an idea of adding the first item, and everytime it occurs i add it or add a count, when it's a create collection when its a, add it, when its b create new collection, and so on i have a collection of each value and i can easily print the count/total count and the name
       next ca
       for each cb in class.b

       next cb
       for each cc in class.c

       next cc
     next class

任何建议表示赞赏,我是新人,到目前为止我有两个菜鸟问题:) 都没有解决不知道为什么:P

4

1 回答 1

0

运行以下程序,等待最后弹出消息框。它会告诉你细节

结果

Sub Percentage()

    Dim a As Collection
    Set a = New Collection

    a.Add "a"
    a.Add "a"
    a.Add "a"
    a.Add "a"
    a.Add "b"
    a.Add "b"
    a.Add "b"
    a.Add "b"
    a.Add "c"
    a.Add "c"
    a.Add "c"
    a.Add "c"
    a.Add "c"
    a.Add "c"
    a.Add "c"

    Dim i As Long, cnt As Long
    cnt = 0
    ' checking the percentage of occurance of letter 'a'
    For i = 1 To a.Count
        If a.Item(i) = "a" Then cnt = cnt + 1
    Next i

    MsgBox "'a' occurs " & cnt & " times in collection A" & _
    vbCrLf & vbCrLf & " which is " & Format((cnt / a.Count), "0.0%") & " of the entire collection"

End Sub
于 2013-08-23T08:17:57.113 回答