lundi 24 juillet 2017

Explain Code (if statement with arrays)

This code prints out the biggest temperature fluctuation of the array in two consecutive days.

But I don't really understand, what happens in the if statements.

Would someone please be so kind to explain it to me?

public class NurTests {

public static void main(String[] args) {

    int[] temperature = { 12, 14, 9, 12, 15, 16, 15, 15, 11, 8, 13, 13, 15, 12 };

    int maxTempDiff = 0;
    int foundDay = 0;
    for (int i = 0; i < temperature.length; i++) {
        int newMaxDiff = 0;
        if ((i + 1) < temperature.length) {
            if (temperature[i] < temperature[i + 1]) {
                newMaxDiff = temperature[i + 1] - temperature[i];
            }
            if (temperature[i] >= temperature[i + 1]) {
                newMaxDiff = temperature[i] - temperature[i + 1];
            }
            if (maxTempDiff < newMaxDiff) {
                maxTempDiff = newMaxDiff;
                foundDay = i;
            }
        }
    }
}

}

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire