Visual Basic:计算返回无穷大的平均函数

时间:2015-06-08 17:25:49

标签: vb.net function visual-studio-2012

我应该计算平均单词数,然后将它们转换为百分比,但是它们显示无穷大。我正在使用的代码如下。

Public Class Form1

    Private Structure advertisements
        Public name As String
        Public words As Integer
        Public font() As fonts
        Public mostLegible As String
    End Structure

    Private Structure fonts
        Public name As String
        Public NoWords() As Integer
        Public aveWords As Double
        Public percent As Double
    End Structure

    Private noAdverts As Integer
    Private noFonts As Integer
    Private noReaders As Integer
    Private advert() As advertisements

    Private Sub GridPlacement(ByVal r As Integer, ByVal c As Integer, ByVal t As String)
        grdDisplay.Row = r
        grdDisplay.Col = c
        grdDisplay.Text = t
    End Sub

    Private Sub gridAndArraySettings()
        Dim x, y As Integer
        ReDim advert(noAdverts)
        For x = 1 To noAdverts
            ReDim advert(x).font(noFonts)
            For y = 1 To noFonts
                ReDim advert(x).font(y).NoWords(noReaders)
                'GridPlacement(0, y, "font " & CStr(y))
            Next
        Next
        grdDisplay.Cols = noFonts + 2
        grdDisplay.Rows = noAdverts + 1

        GridPlacement(0, 0, "Name of advert")


    End Sub

    Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
        noAdverts = CInt(txtadverts.Text)
        noReaders = CInt(txtReaders.Text)
        noFonts = CInt(txtFonts.Text)

        gridAndArraySettings()
    End Sub


    Private Sub btnInput_Click(sender As Object, e As EventArgs) Handles btnInput.Click
        Dim x, y, z As Integer
        Dim total(,) As Integer
        ReDim total(noAdverts, noFonts)

        For x = 1 To noAdverts
            advert(x).name = InputBox("please enter the name of advert " & CStr(x))
            advert(x).words = CInt(InputBox("please enter the number of words for " & advert(x).name))
            GridPlacement(x, 0, advert(x).name)
            For y = 1 To noFonts
                advert(x).font(y).name = InputBox("please enter the name of font " & CStr(y) & " Used in advert " & CStr(x))
                For z = 1 To noReaders
                    advert(x).font(y).NoWords(z) = CInt(InputBox("please enter the number of words understood by reader " & CStr(z) & " for font " & advert(x).font(y).name & " in advert " & advert(x).name))
                    total(x, y) += advert(x).font(y).NoWords(z)
                Next
                advert(x).font(y).aveWords = calcAve(total(x, y))
                advert(x).font(y).percent = advert(x).font(y).aveWords * 100

                GridPlacement(x, y, CStr(advert(x).font(y).aveWords)) 'Format(advert(x).font(y).percent, "###%")
            Next
        Next
    End Sub

    Private Function calcAve(ByVal elements As Integer) As Double
        Dim x, y As Integer
        Dim placeholder As Double = 1

        Select Case elements
            Case Is > 0
                For x = 1 To noAdverts
                    For y = 1 To noFonts
                        advert(x).font(y).aveWords = (elements / noReaders) / advert(x).words
                        placeholder = advert(x).font(y).aveWords
                        MsgBox(CStr(placeholder))
                    Next y
                Next x
            Case Is = 0
                MsgBox("There is incorrect values ")
        End Select
        Return placeholder
    End Function
End Class

文本框名为txtadvertstxtfontstxtreaders

按钮btnsubmit从文本框中获取输入并将它们存储在变量中。 btninput允许来自用户的输入。

GrdDisplay是我大学使用的自定义扩展程序,它是用于显示值的2D网格。

0 个答案:

没有答案