Pergunta

Could someone point out what's wrong with this code. it's not giving me correct max and min value from five input numbers.. (Using IF..)

    Dim arr() As Integer = {1, 2, 3, 4, 5}
    Dim max As Integer = arr(0)
    Dim min As Integer = arr(0)
    arr(0) = InputBox("Enter 1st Value")
    arr(1) = InputBox("Enter 2nd Value")
    arr(2) = InputBox("Enter 3rd Value")
    arr(3) = InputBox("Enter 4th Value")
    arr(4) = InputBox("Enter 5th Value")
    For i As Integer = 1 To arr.Length - 1
        If arr(i) > max Then
            max = arr(i)
        End If
        If arr(i) < min Then
            min = arr(i)
        End If
        TextBox1.Text = max
        TextBox2.Text = min
    Next
Foi útil?

Solução

For a start, you probably want to set min and max after you have entered the first element of the array.

For example, you currently set min to 1 since that's the first element in the array before you start data entry.

If you then enter five values all greater than a hundred, min will remain as 1 despite the fact you didn't enter it.

You're probably looking for something like:

Dim arr(4) As Integer // 0-4 inclusive (from memory)

Dim max As Integer
Dim min As Integer

arr(0) = InputBox("Enter 1st Value")
arr(1) = InputBox("Enter 2nd Value")
arr(2) = InputBox("Enter 3rd Value")
arr(3) = InputBox("Enter 4th Value")
arr(4) = InputBox("Enter 5th Value")

max = arr(0)
min = arr(0)

For ...
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top