Home » Java » java – My code doesn't calculate min and max numbers-Exceptionshub

java – My code doesn't calculate min and max numbers-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I’m trying to write a code which will show the highest, lowest, the difference of them and the average of inputted 30 numbers.
But its not working and is showing the same result for both min and max numbers. Here is the code.

public class aa {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int[] daystemp = new int[30];
        int i = 0;
        int dayHot = 0;
        int dayCold = 0;
        while(i < daystemp.length){
            daystemp[i] = input.nextInt();
            i++;
        }
        int maxTemp = daystemp[0];
        while (i < daystemp.length) {
            if (daystemp[i] > maxTemp) {
                maxTemp = daystemp[i];
                dayHot = i + 1;
                i++;
            }
        }
        System.out.println(maxTemp);
        int minTemp = daystemp[0];
        while (i < daystemp.length) {
            if (daystemp[i] < minTemp) {
                minTemp = daystemp[i];
                dayCold = i + 1;
                i++;
            }
        }
        System.out.println(minTemp);
        int diff = maxTemp - minTemp;
        System.out.println("The difference between them is"+diff);
        double sum = 0;
        while(i < daystemp.length) {
            sum += daystemp[i];
            i++;
        }
        double average = sum / daystemp.length;
        System.out.println("Average was"+average);
    }
}
How to&Answers:

After the first loop (the input loop), i value is daystemp.length (i.e. 30).

It’s never reset to 0. So each while loop condition is false.

Add i=0 before the loops and do i++outside the ifblocks or your code will never end.

example:

    i=0;
    int maxTemp = daystemp[0];
    while (i < daystemp.length) {
        if (daystemp[i] > maxTemp) {
            maxTemp = daystemp[i];
            dayHot = i + 1;
        }
        i++;
    }

Answer:

A few notes about this solution:

  1. By declaring the cumulative total double, no casting is required.
    Because Java knows you want to convert int to double automatically if you assign an int to a declared double. Similary the fact that you want to express a result as double is implied when dividing a double by an int, such as when the average is taken. That avoids a cast also. If you had two ints and you wanted to produce a double you’d need to cast one or more of them, or in cases like a print statement where the compiler can’t deduce the optimal type for the parameter, you’d need to explicitly cast to covert an int value to a double.

  2. Not sure what OS you’re running this on. The ideal situation would be to make it work on all platforms without requiring people type a magic word to end input (because how tacky). The easiest way to end input is to use the OS-specific end of input (end of file) key combination, and for Linux it’s CTRL/D, which is how I explained it in the prompt. On another OS with a different end of input sequence you could just change the prompt. The trickiest would be if it is supposed to be truly portable Java. In that case I’d personally investigate how I could figure out the OS and/or End of File character or key combination on the current OS and modify the prompt to indicate to end input with whatever that is. That would be a bit of and advanced assignment but a very cool result.

  3. Example illustrates use of a named constant to determine the array and is used limit the amount of input (and could be used to limit loop count of for loops accessing the array).

  4. By setting the min and max to very high and low values respectively (notice the LOW value assigned to max and HIGH value assigned to min, those ensure the first legit temp entered will set the min and max and things will go from there).

Temperature Maximum, Minimum, Average and Difference Calculator

import java.util.Scanner;

  public class TemperatureStats {

    final static int MAX_DAYS = 31;

    public static void main(String[] args) {

        int[] dayTemps = new int[MAX_DAYS];
        double cumulativeTemp = 0.0;
        int minTemp = 1000, maxTemp = -1000;  

        Scanner input = new Scanner(System.in);

        System.out.println("Enter temps for up to 1 month of days (end with CTRL/D):");        
        int entryCount = 0;
        while (input.hasNextInt() && entryCount < MAX_DAYS)
            dayTemps[entryCount++] = input.nextInt();

        /* Find min, max, cumulative total */
        for (int i = 0; i < entryCount; i++) {
            int temp = dayTemps[i];
            if (temp < minTemp)
                minTemp = temp;
            if (temp > maxTemp)
                maxTemp = temp;
            cumulativeTemp += temp;
        }

        System.out.println("High temp. = " + maxTemp);
        System.out.println("Low temp.  = " + minTemp);
        System.out.println("Difference = " + (maxTemp - minTemp));
        System.out.println("Avg temp.  = " + cumulativeTemp / entryCount);
    }
}