如何从数组中获取最大值并将其分配给其他数组参数? [英] How to get the max value from the array and assign it to other array parameters?
问题描述
从数组中获取最大值并将其与数组的其他元素匹配的最佳方法是什么?
数组中的数据示例:
Lisa 76
John 82
Kate 59
Peter 74
我可以从阵列中获得最大值,但它没有为其分配正确的学生名称。
我有以下阵列和简化结构:
What is the best way to get the max value from the array and match it to other elements of the array?
Data example in array:
Lisa 76
John 82
Kate 59
Peter 74
I can get the max value from the array but it doesn''t assign the correct name of the student to it.
I have following array and simplified structure:
Dim students(3) As student
Structure student
Dim name As String
Dim Average As Double
End Structure
Private Sub btnfindMax_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnfindMax.Click
Dim i As Integer
Dim max As Double
max = students(i).Average
For i = 0 To Len(students(i).Average) - 1
If students(i).Average > max Then
max = students(i).Average
End If
Next
' A correct max value is assigned to incorrect student name
MsgBox(students(i).name & " " & max)
End Sub
推荐答案
H!,Buddy
您希望学生姓名的最大值,对吗?
您将数据存储在数组或任何其他表或数据集中。
您可以使用该名称轻松找到最大值。
此处无需采用变量平均值。
冷杉你需要找到存储最大值的数组索引,你也称之为GetPostionsOfMaxValue
找到最大值索引时,你得到存储的名字该特定指数的价值。
我希望你能理解,如果有任何问题,请填写我的回答免费评论。
快乐编码好友。
:)
H!, Buddy
You want to max value with its student name, right ?
You store you data in array or any other tables or dataset.
You can find easily max value with that name.
Here no need to take variable Average.
First you need to find that Index of array in which stored max value, you also called that GetPostionsOfMaxValue
When you find index of max value, on that you get stored name and value of that particular index.
I hope you will understand, if any question so fill free comment on my answer.
Happy Coding Buddy.
:)
这一行毫无意义,不会用普通语言编译:
This line makes no sense and would not compile in "normal" language:
max = students(i).Average
First of所有 i
未分配;第二,你不应该声明它,最后,获取数组的任何元素的初始值 max
不会做出正确的算法。而是这样做:
First of all i
is not assigned; second of all, you should not declare it, and finally, getting any element of the array for initial value of max
won''t make a correct algorithm. Instead do this:
max = double.NegativeInfinity
此特殊值将使用''<''运算符(和其他比较运算符)与除NaN之外的任何其他double值进行正确比较,并将作为最大值的正确初始值。确实,不会有任何数字少于double.NegativeInfinity。
如果你还需要使用名称
(或 student
结构的其他成员),您不仅需要搜索最大值,还要记住最大元素的索引。如果你想一点,你将很容易修改它得到的代码。 :-)
This special value will correctly compare using ''<'' operator (and other comparison operators) with any other double value except NaN and will serve as a correct initial value for maximum. Indeed, there won''t be any number less then double.NegativeInfinity.
If you also need to use the name
(or some other member of the student
structure), you need to search not just for maximum, but also remember the index of the maximum element. If you think just a bit, you will easily modify the code the get it. :-)
这篇关于如何从数组中获取最大值并将其分配给其他数组参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!