使用vb.net查找数组中的最小值和最大值

时间:2022-09-28 10:59:03

I need to find the min and max value in an array. The .max function works but .min keeps showing zero.

我需要在数组中找到最小值和最大值。 .max函数有效,但.min保持显示为零。

Public Class Program_2_Grade
    Dim max As Integer
    Dim min As Integer
    Dim average As Integer
    Dim average1 As Integer
    Dim grade As String
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If TextBox1.Text = Nothing Or TextBox1.Text > 100 Then
            MsgBox("Doesn't Meet Grade Requirements", MsgBoxStyle.Exclamation, "Error")
            TextBox1.Clear()
            TextBox1.Focus()
            counter = 0
        Else
            grade_enter(counter) = TextBox1.Text
            TextBox1.Clear()
            TextBox1.Focus()
            counter = counter + 1

            If counter = grade_amount Then
                max = grade_enter.Max()
                min = grade_enter.Min()

                For i As Integer = 0 To counter
                    average = average + grade_enter(i) / counter
                    average1 = average1 + grade_enter(i) - grade_enter.Min / counter
                Next

                Select Case average
                    Case 30 To 49
                        grade = "C"
                    Case 50 To 69
                        grade = "B"
                    Case 70 To 100
                        grade = "A"
                    Case Else
                        grade = "Fail"
                End Select

                If (Program_2.CheckBox1.Checked = True) Then
                    Program_2.TextBox4.Text = _
                ("Name:" & " " & (Program_2.TextBox1.Text) & vbNewLine & _
                "Class: " & (Program_2.TextBox2.Text) & vbNewLine & _
                "Number Of Grades:" & " " & (Program_2.TextBox3.Text) & vbNewLine & _
                "Max:" & " " & max & vbNewLine & _
                "Min:" & " " & min & vbNewLine & _
                "Average:" & " " & average1 & vbNewLine) & _
                "Grade:" & " " & grade & vbNewLine & _
                "Dropped Lowest Grade"
                Else
                    Program_2.TextBox4.Text = _
                ("Name:" & " " & (Program_2.TextBox1.Text) & vbNewLine & _
                "Class: " & (Program_2.TextBox2.Text) & vbNewLine & _
                "Number Of Grades:" & " " & (Program_2.TextBox3.Text) & vbNewLine & _
                "Max:" & " " & max & vbNewLine & _
                "Min:" & " " & min & vbNewLine & _
                "Average:" & " " & average & vbNewLine) & _
                "Grade:" & " " & grade & vbNewLine
                End If

                Me.Close()
                average = 0
                average1 = 0
                counter = 0
            End If
        End If
    End Sub

My arrays are set at global scope.

我的数组设置在全局范围内。

4 个解决方案

#1


5  

You haven't shown where grade_enter is being created. My guess is that it's bigger than it needs to be, so there are "empty" entries (with value 0) which are being picked up when you try to find the minimum.

您尚未显示正在创建grade_enter的位置。我的猜测是它比它需要的大,所以当你试图找到最小值时,有“空”条目(值为0)被拾取。

You could change it to:

您可以将其更改为:

max = grade_enter.Take(counter).Max()
min = grade_enter.Take(counter).Min()

as a hacky way of making it work, but it would be better to use the right amount of space to start with (or a List(Of Integer)).

作为一种使其工作的hacky方式,但最好使用适当的空间来开始(或List(Of Integer))。

#2


3  

Stocksy101:

As others have mentioned, your array's initial values will be 0, so if you are creating an array larger than necessary, Min() will always return 0.

正如其他人所提到的,您的数组的初始值将为0,因此如果您创建的数组大于必要数量,则Min()将始终返回0。

Additionally, a cute little quirk of Visual Basic .NET is that when you declare an array such as:

另外,Visual Basic .NET的一个可爱的小怪癖就是当你声明一个数组时,例如:

Public grade_enter(20) As Integer

You're actually creating a 21-item array, not a 20-item array. (VB declares arrays as their upper bound.) (See StartVBDotNet.) So that might have something to do with it.

你实际上是在创建一个21项数组,而不是20项数组。 (VB将数组声明为其上限。)(参见StartVBDotNet。)这可能与它有关。

In any event, if you're on VB.NET 2005 or 2008, you might consider looking into the List(Of Integer) class. (This is actually just the List class; it's what's called "generic.") This class will allow you dynamically-sized arrays, which grow or shrink based on the items added to them. It doesn't, unfortunately, have Min() and Max() methods, but it does have a ToArray() method, from which you could then run the Min() and Max() methods.

无论如何,如果您使用的是VB.NET 2005或2008,您可以考虑查看List(Of Integer)类。 (这实际上只是List类;它是所谓的“泛型”。)此类将允许动态调整大小的数组,这些数组会根据添加到它们的项目增大或缩小。遗憾的是,它没有Min()和Max()方法,但它确实有一个ToArray()方法,然后您可以从中运行Min()和Max()方法。

#3


0  

I'm having a hard time finding where you defined grade_enter(). That code would easier to read if you broke it up into a few smaller methods. But I'm guessing you defined it as an array of integers with a static size that's large enough to hold however many items your professor told you to expect. In that case, any unset item has a value of 0, which will be smaller than any grades that were entered. You need to account for that, perhaps by using a List(Of Integer) rather than an array.

我很难找到你定义grade_enter()的地方。如果你把它分解成几个较小的方法,那么代码会更容易阅读。但我猜你把它定义为一个整数数组,其静态大小足以容纳你教授告诉你的许多项目。在这种情况下,任何未设置的项目的值都将为0,这将小于输入的任何等级。您需要考虑到这一点,可能使用List(Of Integer)而不是数组。

#4


0  

I have an additional question from above. How can I get the index number of the array element in grade_enter() which has the .Max() value (from above):

我还有一个问题。如何获取grade_enter()中具有.Max()值(从上面)的数组元素的索引号:

max = grade_enter.Max() ' .... = grade_enter(Index_number)

max = grade_enter.Max()'.... = grade_enter(Index_number)

Possible code: dim index_number as integer = grade_enter.Max.Index

可能的代码:dim index_number as integer = grade_enter.Max.Index

I looked in the MS library but didn't see a suitable index function.

我查看了MS库,但没有看到合适的索引函数。

Currently to get the index, I use a 'do until' loop to search grade_enter() for this max value. thanks

目前为了获得索引,我使用'do until'循环来搜索grade_enter()以获得此最大值。谢谢

#1


5  

You haven't shown where grade_enter is being created. My guess is that it's bigger than it needs to be, so there are "empty" entries (with value 0) which are being picked up when you try to find the minimum.

您尚未显示正在创建grade_enter的位置。我的猜测是它比它需要的大,所以当你试图找到最小值时,有“空”条目(值为0)被拾取。

You could change it to:

您可以将其更改为:

max = grade_enter.Take(counter).Max()
min = grade_enter.Take(counter).Min()

as a hacky way of making it work, but it would be better to use the right amount of space to start with (or a List(Of Integer)).

作为一种使其工作的hacky方式,但最好使用适当的空间来开始(或List(Of Integer))。

#2


3  

Stocksy101:

As others have mentioned, your array's initial values will be 0, so if you are creating an array larger than necessary, Min() will always return 0.

正如其他人所提到的,您的数组的初始值将为0,因此如果您创建的数组大于必要数量,则Min()将始终返回0。

Additionally, a cute little quirk of Visual Basic .NET is that when you declare an array such as:

另外,Visual Basic .NET的一个可爱的小怪癖就是当你声明一个数组时,例如:

Public grade_enter(20) As Integer

You're actually creating a 21-item array, not a 20-item array. (VB declares arrays as their upper bound.) (See StartVBDotNet.) So that might have something to do with it.

你实际上是在创建一个21项数组,而不是20项数组。 (VB将数组声明为其上限。)(参见StartVBDotNet。)这可能与它有关。

In any event, if you're on VB.NET 2005 or 2008, you might consider looking into the List(Of Integer) class. (This is actually just the List class; it's what's called "generic.") This class will allow you dynamically-sized arrays, which grow or shrink based on the items added to them. It doesn't, unfortunately, have Min() and Max() methods, but it does have a ToArray() method, from which you could then run the Min() and Max() methods.

无论如何,如果您使用的是VB.NET 2005或2008,您可以考虑查看List(Of Integer)类。 (这实际上只是List类;它是所谓的“泛型”。)此类将允许动态调整大小的数组,这些数组会根据添加到它们的项目增大或缩小。遗憾的是,它没有Min()和Max()方法,但它确实有一个ToArray()方法,然后您可以从中运行Min()和Max()方法。

#3


0  

I'm having a hard time finding where you defined grade_enter(). That code would easier to read if you broke it up into a few smaller methods. But I'm guessing you defined it as an array of integers with a static size that's large enough to hold however many items your professor told you to expect. In that case, any unset item has a value of 0, which will be smaller than any grades that were entered. You need to account for that, perhaps by using a List(Of Integer) rather than an array.

我很难找到你定义grade_enter()的地方。如果你把它分解成几个较小的方法,那么代码会更容易阅读。但我猜你把它定义为一个整数数组,其静态大小足以容纳你教授告诉你的许多项目。在这种情况下,任何未设置的项目的值都将为0,这将小于输入的任何等级。您需要考虑到这一点,可能使用List(Of Integer)而不是数组。

#4


0  

I have an additional question from above. How can I get the index number of the array element in grade_enter() which has the .Max() value (from above):

我还有一个问题。如何获取grade_enter()中具有.Max()值(从上面)的数组元素的索引号:

max = grade_enter.Max() ' .... = grade_enter(Index_number)

max = grade_enter.Max()'.... = grade_enter(Index_number)

Possible code: dim index_number as integer = grade_enter.Max.Index

可能的代码:dim index_number as integer = grade_enter.Max.Index

I looked in the MS library but didn't see a suitable index function.

我查看了MS库,但没有看到合适的索引函数。

Currently to get the index, I use a 'do until' loop to search grade_enter() for this max value. thanks

目前为了获得索引,我使用'do until'循环来搜索grade_enter()以获得此最大值。谢谢