Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net VB中的代码帮助数组和结构变量_Vb.net - Fatal编程技术网

Vb.net VB中的代码帮助数组和结构变量

Vb.net VB中的代码帮助数组和结构变量,vb.net,Vb.net,我不知道从这里到哪里去。我想我已经初始化了一个二维数组,并用用户输入填充了它,但我无法使它达到平均值。那么,有没有一种方法可以分别对这个数组的行进行平均 此外,如果有更好的方法来编写此代码,那么从任何帮助开始都将是非常好的。我还将把原始问题贴在代码的底部,就在书里 Public Class frmMain Structure TestScores Public decTest1 As Decimal Public decTest2 As Decimal Public d

我不知道从这里到哪里去。我想我已经初始化了一个二维数组,并用用户输入填充了它,但我无法使它达到平均值。那么,有没有一种方法可以分别对这个数组的行进行平均

此外,如果有更好的方法来编写此代码,那么从任何帮助开始都将是非常好的。我还将把原始问题贴在代码的底部,就在书里

Public Class frmMain

Structure TestScores
    Public decTest1 As Decimal
    Public decTest2 As Decimal
    Public decTest3 As Decimal
End Structure
Dim test(2) As TestScores
Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    test(0).decTest1 = InputBox("Score 1:", "First Person")
    test(0).decTest2 = InputBox("Score 2:", "First Person")
    test(0).decTest3 = InputBox("Score 3:", "First Person")
    test(1).decTest1 = InputBox("Score 1:", "Second Person")
    test(2).decTest2 = InputBox("Score 2:", "Second Person")
    test(2).decTest3 = InputBox("Score 3:", "Second Person")
    test(3).decTest1 = InputBox("Score 1:", "Third Person")
    test(3).decTest2 = InputBox("Score 2:", "Third Person")
    test(3).decTest3 = InputBox("Score 3:", "Third Person")


End Sub

Private Function GetAverage(ByVal scores As TestScores) As Decimal
    ' calculates and returns the average score

    Dim decTotal As Decimal

    decTotal = scores.decTest1 + scores.decTest2 + scores.decTest3
    Return decTotal / 3
End Function

Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
    Me.Close()
End Sub


Private Sub btnAverage_Click(sender As Object, e As EventArgs) Handles btnAverage.Click
    ' displays the average test score
    Dim max1 As Integer = 0
    Dim max2 As Integer = 0
    Dim max3 As Integer = 0

    max1 = (test(0) + test(1))

End Sub
末级

图书问题: 打开“代码编辑器”窗口。btnAverage_Click过程应声明 结构变量的三元数组;每个元素将包含测试分数 一个学生。程序应提示用户进行学生的三项测试 得分,然后将得分存储在数组中的一个结构变量中。那么 应使用该函数确定学生的平均分数。最后,它应该 显示学生的数字(1、2或3)以及平均分数(格式化) 在lblResult控件中输入一个小数位)。保存解决方案,然后启动并重试 测试应用程序。图21-14显示了当 用户输入以下分数:100、100、100、90、85、78、73、72和67。关闭
“代码编辑器”窗口,然后关闭解决方案。

不需要单独的函数。只需计算数组中每个元素的平均值,
max1=(test(0)。decTest1+test(0)。decTest2+test(0)。decTest3)\3


因为您将max1声明为整数,所以我使用integer division()自动将答案格式化为整数。

从技术上讲,这不是二维数组。它是一个结构的一维数组,碰巧有三个值。而且,你的书在教你使用变量前缀方面有点过时。现代风格的指导方针特别建议不要使用它们(除了在不同的gui控件类型中使用它们通常很有用)?