【问题标题】:Need help can figure out this project in VB需要帮助可以在VB中搞清楚这个项目
【发布时间】:2011-12-16 00:35:53
【问题描述】:

这是我到目前为止所拥有的。似乎无法让它工作这些是指导方针

***细节 您将使用 1 个文件输入所有分数。该文件名为 Data.txt,应存储在项目 Debug 文件夹中。分数是浮点数。

一个按钮应该计算平均值、范围和标准偏差并显示它们。 您必须使用单独的函数来计算这 3 个统计数据。

一个按钮应该以表格形式显示频率。对于这个练习,我们感兴趣的频率如下:

# scores < 60
60 <= # scores < 70
70 <= # scores < 80
80 <= # scores < 90
90 <= # scores

您必须使用单独的数组来保存各个范围的总计。

所有分数都应显示在列表框中。*

Option Strict On
Public Class Form1

    Private names() As String = IO.File.ReadAllLines("data.txt")
    Private scores(names.Count - 1) As double

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For i As Integer = 0 To names.Count - 1
            scores(i) = CInt(names(i))
        Next
    End Sub
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Me.Close()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim sum As Double = 0
        mean(sum)
        OutputListBox.Items.Add(sum)
    End Sub

    Function mean(ByRef sum As Double) As Double
        Dim total As Double = scores(0)

        For i As Double = 0 To scores.Count - 1
            sum = 
        Next
        Return sum
    End Function

End Class

【问题讨论】:

  • 你有什么问题?
  • 我根本想不通。无法从 txt 文件中获取平均值、范围或标准差。并且文本文件可以包含任何类型的“测试”等级编号。
  • 嗯,这将很难提供帮助。您为mean() 函数发布的代码不执行任何操作(或编译,使用不完整的表达式sum =)。您必须至少描述您遇到的具体问题,以便我们为您提供帮助。
  • 我知道这就是全部。我什么都不明白。我无法让这些功能正常工作。我需要单击按钮将 data.txt 文件传递​​给三个不同的函数来计算平均值、范围和标准差。

标签: vb.net


【解决方案1】:

我不会为你做所有的工作,但这里是对你的表单的重组,其中实现了平均值和一些 cmets,这样你就可以弄清楚发生了什么:

Option Strict On
Option Explicit On

Partial Public Class Form1

    Private scores() As Double

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            ' Clear the list view
            ListView1.Clear()

            ' First, load the scores from the file into memory
            Call LoadScores()

            Dim value As Double

            ' Next, calculate the mean
            value = CalculateMean()
            ' And add it to the list view
            ListView1.Items.Add("Mean = " & value.ToString)

            ' Now calculate the other items and display them


        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    ''' <summary>
    ''' This method loads the scores from the file into the scores array
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub LoadScores()

        Dim stringScores() As String

        ' Read all of the scores in the file into an array of strings
        stringScores = IO.File.ReadAllLines("data.txt")

        ' Resize the scores array to hold all of the values in the file
        ReDim scores(stringScores.Length)

        Dim counter As Integer

        ' Now convert those strings to numbers, and store them in scores
        For Each sValue As String In stringScores
            ' Convert the string value from the file into a double and store it in the current location in the scores array
            scores(counter) = CDbl(sValue)
            ' Keep track of our position in the scores array
            counter += 1
        Next
    End Sub

    ''' <summary>
    ''' The mean is the sum of the scores divided by the total number of scores
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function CalculateMean() As Double

        If scores.Length <> 0 Then
            Dim total As Double

            For Each value As Double In scores
                total += value
            Next

            Return total / scores.Length
        Else
            ' Avoid a divide by zero error
            Return 0
        End If
    End Function

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Me.Close()
    End Sub


End Class

【讨论】:

  • 非常感谢!你能告诉我方程的范围和标准差吗?!
  • 你研究过如何计算标准差吗?我通过快速谷歌搜索找到了一些建议。如果您尝试实施它怎么样,那么我们可以看到需要修复的内容。
【解决方案2】:

与任何问题一样,首先将其分解并一次做一件。一种常见的方法是让一个函数实现一个(或一个)需求的一部分,所以从那里开始。

  1. 读取文件以获取分数。在本练习中,您有一个硬编码的名称,但让文件读取函数将名称作为参数同样容易。您知道分数被列为浮点数,因此您将它们返回为DoubleSingle。我看到你在到目前为止的代码中使用了一个数组,所以我们会坚持下去。有了这些信息,我们就可以对函数进行签名了

    Public Function ReadScoresFromFile(fileName As String) As Double()
    
  2. 计算均值、极差和标准差;三件事,三个功能。根据这三件事的数学定义,您将需要接受一系列数字并返回一个数字的函数。这应该会引导您找到函数定义:

    Public Function Mean(values As Double()) As Double
    Public Function Range(values As Double()) As Double
    Public Function StandardDeviation(values As Double()) As Double
    

    在线或通过您的数学/统计书籍快速搜索应该会为您提供可以转换为代码的定义和公式。 提示:您可以从其他函数中调用一个函数来简化工作。

  3. 将分数分组。与文件名一样,您有一个指定的范围列表。您可以尝试将函数参数化以将范围作为参数,但这可能不值得为此练习付出努力。因此,我们知道有一个分数序列,并且需要每个范围内的总分数列表。由于总计将是整数而不是浮点数,因此我们将相应地选择类型。

    Public Function CountInRanges(scores as Double()) As Integer()
    

此时,您拥有获取所需数据所需的所有功能。现在您只需让各种按钮单击处理程序调用适当的函数并将结果放在适当的标签、列表框等中。使用参数和返回类型来帮助您确定从一个函数到下一个函数的数据流,根据需要使用变量来保存中间结果。

我从您展示的代码中注意到了一些事情(这些都不是解决您眼前的问题所必需的,但现在和将来可能会有所帮助。):

  • 尝试为您的控件指定有意义的名称。 AggregatesButtonRangesButtonMeanLabel 等比Button1Button2 等更容易记住。(名称也有多种样式。有些人会使用前缀而不是后缀btnAggregatesbtnRangeslblMean 等名称。)
  • 数字类型都有一个共享的Parse 方法。您可以考虑使用它而不是转换,因为它允许您指定有关如何从字符串中解析数字的几个选项。目前,转换可能就足够了,但这至少是以后要隐藏的东西。
  • 我不知道您的课程稍后会涵盖哪些主题,但您可以考虑查看For Each 循环而不是标准的 For 循环。当您需要为序列中的每个项目执行某些操作但不关心索引时,这可能很有用,例如 Mean、Range、StandardDeviation 和 CountInRanges 计算。这可以使您的代码更清晰,因为您不会一直引用数组,并且可以更轻松地转换为以后可能遇到的其他序列类(例如:List、Collection 等)。它还可以防止您意外修改数组,而您在任何这些函数中都不想这样做。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-16
    • 2020-09-03
    相关资源
    最近更新 更多