当前位置:  开发笔记 > 后端 > 正文

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

如何解决《使用vb.net查找数组中的最小值和最大值》经验,为你挑选了1个好方法。

我需要在数组中找到最小值和最大值.该.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

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



1> Jon Skeet..:

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

您可以将其更改为:

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

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

推荐阅读
Chloemw
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有