dimanche 22 novembre 2015

Trying to get maxNumber out of an array, fails to reach the last element

I am trying to get a GUI set up with a FlowLayout some JLabels, JTextFields and JButtons. Six JTextFields, five of them to get input by the user, input must a number, last JTextField shows the result depending on which button is clicked. JButtons: average, minimum, maximum. At the moment, when I try to get the maximum number it works up the fourth field. If there is a higher value at fifth field, it won’t show as the highest value.

I used a for loop that iterates though an array containing all numbers. Followed by an if condition that looks like this:

if(numbers[i]>maxValue)

where maxValue is set at zero.

public void getMax()
{
    double maxValue=0;
    double numbers[] = {num1,num2,num3,num4,num5};

    for(int i=0;i<=numbers.length-1;i++)
    {
        if(numbers[i]>maxValue)
        {
            maxValue = numbers[i];  
        }
    }
    textResult = String.valueOf(maxValue);
    resultField.setText(textResult);
}

Aucun commentaire:

Enregistrer un commentaire