Visual Basic:在数组中查找最大数目并显示结果 [英] Visual Basic: Finding the Maximum Number in an Array and Displaying the Result
问题描述
已经给了我一个算法,然后我必须编写一个程序,允许用户将8个数字输入到数组中.每次输入数字时,索引都会递增,并根据输入的数字找到新的最大值.
该算法按原样运行:
I have been given an algorithm which I then have to make a program for which allows the user to enter in 8 Numbers into an array. Each time a number is entered, the index is incremented and a new maximum is found based on the numbers entered.
The algorithm goes as is:
BEGIN
Set Index to 1
Set ‘Maximum’ to 0
WHILE Index < 8
IF Number(Index) > Maximum THEN
Maximum = Number(Index)
MaxIndex = Index
ENDIF
Increment Index
ENDWHILE
Display Maximum
END
到目前为止,我对代码尚有想法,但是由于此代码无法正常运行,因此我无法完成任务.到目前为止,我有这个:
I have got an idea about the code so far but I am unable to complete the task as this code does not function properly. So far I have this:
Public Class Form1
Dim Maximum As Integer
Dim Num As Integer
Dim Index As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Maximum = 0
txtNumber.Text = Num
End Sub
Private Sub txtShoeSize_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtShoeSize.KeyPress
If Not (Char.IsNumber(e.KeyChar) Or Char.IsControl(e.KeyChar)) Then e.Handled = True
End Sub
Private Sub btnEnter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnter.Click
If Index = 7 Then
MsgBox("Maximum amount of data entered, reset to add more.")
ElseIf txtShoeSize.Text = "" Then
MsgBox("Enter a Number.")
ElseIf Num >= Maximum Then
Maximum = Num
NumArray.Items.Add(txtNum.Text)
Index = Index + 1
Else
Index = Index + 1
NumArray.Items.Add(txtNum.Text)
End If
txtNum.Clear()
End Sub
Private Sub btnDisplayMax_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayMax.Click
MsgBox("Maximum Value: " & Maximum)
End Sub
End Class
我真的很需要此代码的帮助.谢谢
I really urgently need help with this code. Thanks
推荐答案
检查一下
重新审视,优化和稳定的QuickSort [
Check this out
QuickSort Revisited, Optimised, and Stabilised[^]
我将对您编写条件的方式进行一些小的更改,以消除一些多余的代码重复.
I would make a small change in the way you write your conditions so as to remove some redundant duplication of code.
If Index = 7 Then
MsgBox("Maximum amount of data entered, reset to add more.")
ElseIf txtShoeSize.Text = "" Then
MsgBox("Enter a Number.")
Else
If Num >= Maximum Then
Maximum = Num
End If
NumArray.Items.Add(txtNum.Text)
Index = Index + 1
End If
如果您提供有关无法正常运行"的更多信息,那将有助于我们提供更多帮助.向我们描述实际发生的情况与应该发生的情况.
If you provide a little more information as to the "does not function properly", that would help us help you more. Describe to us what actually happens versus what should happen.
我找不到在哪里读取变量num的信息,您是否错过了此信息
i cant find where you are read the variable num, are you missing this
If (Integer.TryParse(txtnum) = True) Then
num = Integer.Parse(txtnum)
Else
MessageBox.show("Not A number")
return
end if
这篇关于Visual Basic:在数组中查找最大数目并显示结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!