这是我到目前为止所拥有的。似乎不能使它工作这些是准则
***具体细节 您将使用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
答案 0 :(得分:0)
我不会为你完成所有的工作,但是这里是对你的表单进行重组,实现了平均值和一些注释,以便你可以弄清楚发生了什么:
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
答案 1 :(得分:0)
与任何问题一样,首先将其分解并一次做一件。一种常见的方法是使一个函数实现一个(或一个部分)需求,所以从那里开始。
阅读文件以获得分数。在本练习中,您有一个硬编码的名称,但是使文件读取功能将名称作为参数也很容易。您知道分数会列为浮点数,因此您会将其作为Double
或Single
返回。我看到你在目前的代码中使用了一个数组,所以我们会坚持下去。有了这些信息,我们就可以对函数进行签名
Public Function ReadScoresFromFile(fileName As String) As Double()
计算平均值,范围和标准差;三件事,三件功能。根据这三件事的数学定义,您将需要一系列数字并返回一个数字的函数。这应该引导您进入函数定义:
Public Function Mean(values As Double()) As Double
Public Function Range(values As Double()) As Double
Public Function StandardDeviation(values As Double()) As Double
在线快速搜索或通过数学/统计书进行快速搜索,可以为您提供可以转化为代码的定义和公式。 提示:您可以从其中一个调用一个函数来简化工作。
将分数划分为范围。与文件名一样,您有一个指定的范围列表。您可以尝试参数化函数以将范围作为参数,但它可能不值得为此练习付出努力。因此,我们知道有一系列分数,需要每个范围内的总分数列表。由于总数将是整数而不是浮点数,因此我们将相应地选择类型。
Public Function CountInRanges(scores as Double()) As Integer()
此时,您拥有获取所需数据所需的所有功能。现在您只需要使各个按钮单击处理程序调用相应的函数并将结果放在适当的标签,列表框等中。使用参数和返回类型来帮助您确定从一个函数到下一个函数的数据流,根据需要使用变量来保存中间结果。
我从您展示的代码中注意到的一些事情(这些都不是解决您的问题所必需的,但现在和未来都可能有用。):
AggregatesButton
,RangesButton
,MeanLabel
等比Button1
,Button2
等更容易记住(名称也有各种风格。人们会为btnAggregates
,btnRanges
,lblMean
等名称使用前缀而不是后缀。)Parse
方法。您可以考虑使用此而不是转换,因为它允许您指定有关如何从字符串中解析数字的若干选项。目前,转换可能就足够了,但这至少可以收集起来。