我有一个视觉基本问题。我有一个记录结构,有4个变量,其中一个是数组。我很难找到最大值。该函数为所有冠军返回相同的值。冠军获得最佳分数的日子各不相同,功能返回的值也不同,但不是
Private Structure DebateChamps
' user will input the name of each debate champion
' the user will also input the scores of each champion
' the bestday---day on which a champion attained highest score,
' and average for each champion are for me to work out
Public Name As String
Public Score() As Double
Public Average As Double
Public BestDay As Integer
我已经解决了前三个问题,但现在我找不到最好的一天了。我想用一个函数来做这个。我知道找到最好的一天基本上意味着找到最大值。这就是我找到最好的一天。它的问题是,当我将它输出到网格上时,我每次都得到相同的值。例如,让冠军1有分数
9(第1天),第8天(第2天),第6天(第3天)
和冠军2有分数
7(第1天),第5天(第2天),第9天(第3天)
我将第3天作为两者的最佳日子。请帮忙。
我将数组传递给函数
我希望它返回一个数组,因为有很多天,每个冠军每天可能会有所不同
Private Function ComputeBestDay(ByVal ChampAry() As DebateChamps) As Integer()
Dim Max(nChamps, nDays) As Double
Dim MaxIndex(nChamps) As Integer
Dim r, c As Integer
'I initialise the max values to zero
'and the index as well
For r = 1 To nChamps
For c = 1 To nDays
Max(r, c) = 0
MaxIndex(r) = 0
Next c
Next r
'I try to find the max
'I store the day in MaxIndex
For r = 1 To nChamps
For c = 1 To nDays
If Max(r, c) < ChampAry(r).Score(c) Then
Max(r, c) = ChampAry(r).Score(c)
MaxIndex(r) = c
End If
Next c
Next r
Return MaxIndex
End Function
我调用下面的函数
Private Sub btnBestDay_Click(sender As Object, e As EventArgs) Handles btnBestDay.Click
Dim i As Integer
Dim ans(nChamps) As Integer
'I call the function
For i = 1 To nChamps
ans = ComputeBestDay(AllChampsAry)
AllChampsAry(i).BestDay = ans(i)
placetext(i, nDays + 2, CStr(AllChampsAry(i).BestDay))
Next i
End Sub
答案 0 :(得分:0)
尝试使用LINQ:
Private Function ComputeBestDay(ByVal ChampAry() As DebateChamps) As Integer()
Return ChampAry _
.Select(Function (r) _
Enumerable _
.Range(0, r.Score.Length) _
.First(Function (c) r.Score(c) = r.Score.Max())) _
.ToArray()
End Function
答案 1 :(得分:0)
我明白了。我被指示使用计算最大位置的函数。由于记录结构,我很困惑。在我的记录结构中,我有以下
Private Structure DebateChamps
Public Name As String
Public Score() As Double
Public Average As Double
Public BestDay As Integer
End Structure
让我们说学校校长希望挑选最好的辩论团队----因此结构名称DebateChamps。为此,他将选择平均值大于5的名称。每个冠军的记录都会进入数组
Private AllChampsAry() As DebateChamps
处理输入和平均值是直截了当的。校长还希望找到球员表现最佳的那一天。所以我必须为每个玩家显示maxindex。
Dim max As Double
Dim index(nChamps) As Integer
Dim r, c As Integer
For r = 1 To nChamps
max = 0
For c = 1 To nDays
If max < ChampAry(r).Score(c) Then
max = ChampAry(r).Score(c)
index(r) = c
End If
Next c
Next
首先循环每个冠军(争夺冠军的候选人)然后通过每个冠军的日子(假设每个冠军参加相同的天数)...我将每个冠军的索引存储在一个数组中并返回它。 由于记录结构,我很困惑。我以前从未使用过它们