Vb.net 使用函数记录结构、数组和查找最大值
我有一个visual basic问题。我有一个记录结构,它有4个变量,其中一个是数组。我很难找到最大值。该函数为所有冠军返回相同的值。冠军获得最佳分数的天数各不相同,函数返回的值也不相同,但事实并非如此Vb.net 使用函数记录结构、数组和查找最大值,vb.net,max,user-defined-functions,dynamic-arrays,return-type,Vb.net,Max,User Defined Functions,Dynamic Arrays,Return Type,我有一个visual basic问题。我有一个记录结构,它有4个变量,其中一个是数组。我很难找到最大值。该函数为所有冠军返回相同的值。冠军获得最佳分数的天数各不相同,函数返回的值也不相同,但事实并非如此 Private Structure DebateChamps ' user will input the name of each debate champion ' the user will also input the scores of each champion ' the
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天)
我会把第三天作为两个人最好的一天。请帮忙
我将数组传递到函数中我希望它返回一个数组,因为有很多天,每一天可能会因冠军而异
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
尝试使用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
我想出来了。我被指示使用一个计算最大值位置的函数。由于记录结构,我感到困惑。在我的记录结构中,我有以下几点
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
直接处理输入和平均值。校长还想知道哪一天球员表现最好。所以我必须显示每个玩家的最大索引
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
最大尺寸为双精度
尺寸索引(nChamps)为整数
Dim r,c为整数
对于r=1至nChamps
最大值=0
对于c=1到星期日
如果max
首先遍历每个冠军(辩论冠军候选人),然后遍历每个冠军的天数(假设每个冠军参与的天数相同)…我将每个冠军的索引存储在一个数组中,然后返回它。
由于记录结构,我感到困惑。我以前从未使用过它们。我很难找到最大值。该函数为所有冠军返回相同的值。冠军获得最佳分数的日期各不相同,函数返回的值也不相同,但今天晚些时候将重新发布问题。对不起,这是新来的,真的很累。你需要给我们定义一下你所说的一天的max.max champ是什么意思?全天最多的冠军。。。这和BestDay有什么关系??
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