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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/221804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 13:52:05  来源:igfitidea点击:

Finding min and max values in an array using vb.net

arraysvb.net

提问by John Rudy

I need to find the min and max value in an array. The .maxfunction works but .minkeeps 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.

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

回答by Jon Skeet

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方式,但最好使用适量的空间开始(或a List(Of Integer))。

回答by John Rudy

Stocksy101:

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 Listclass; 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()andMax()方法,但它确实有一个ToArray()方法,您可以从中运行Min()andMax()方法。

回答by Joel Coehoorn

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,这将小于输入的任何成绩。您需要考虑到这一点,也许是使用 aList(Of Integer)而不是数组。