gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 11:41:47 25 4
gpt4 key购买 nike

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

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

最佳答案

您尚未显示grade_enter 的创建位置。我的猜测是它比需要的要大,因此当您尝试找到最小值时,会拾取“空”条目(值为 0)。

您可以将其更改为:

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

作为使其工作的一种巧妙方法,但最好使用适量的空间(或 List(Of Integer))开始。

关于arrays - 使用 vb.net 查找数组中的最小值和最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/221804/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com