viewing paste DisplayWeekTempStat | Java

Posted on the | Last edited on
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
import java.util.Scanner;
 
public  class   DisplayWeekTempStat{
 
    public  static  void    main(String[] args){
        
        int i = 0;
        final int WEEK = 7;
        Scanner input = new Scanner(System.in);
        int[] temperature = new int[WEEK];
        int[] hot = new int[WEEK];
        int[] cold = new int[WEEK];
        
        while(i < 7){
            System.out.println("Please input todays highest temperature in celsius: ");
            temperature[i] = input.nextInt();
            i++;
        }
        System.out.println("The average temperature of the week is: "+averageTemperature(temperature)+" degree(s)");
        System.out.println("The hottest temperature is: "+hottestTemperature(temperature)+" degree(s)");
        System.out.println("The coldest temperature is: "+coldestTemperature(temperature)+" degree(s)");
        hot = searchTemp(temperature,hottestTemperature(temperature));
        cold = searchTemp(temperature,coldestTemperature(temperature));
        System.out.print("The hottest day(s) are: ");
        for(i = 0; i < WEEK; i++){
            switch(hot[i]){
                case 0: System.out.print("Sunday "); break;
                case 1: System.out.print("Monday "); break;
                case 2: System.out.print("Tuesday "); break;
                case 3: System.out.print("Wednesday "); break;
                case 4: System.out.print("Thursday "); break;
                case 5: System.out.print("Friday "); break;
                case 6: System.out.print("Saturday "); break;
            }
            if( i < hot.length-1)
                if(hot[i] > hot[i+1] || hot[i] == hot[i+1])
                    break;
        }
        System.out.print("\nThe coldest day(s) are: ");
        for(i = 0; i < WEEK; i++){
            switch(cold[i]){
                case 0: System.out.print("Sunday "); break;
                case 1: System.out.print("Monday "); break;
                case 2: System.out.print("Tuesday "); break;
                case 3: System.out.print("Wednesday "); break;
                case 4: System.out.print("Thursday "); break;
                case 5: System.out.print("Friday "); break;
                case 6: System.out.print("Saturday "); break;
            }
            if( i < cold.length-1)
                if(cold[i] > cold[i+1] || cold[i] == cold[i+1])
                    break;
        }
        
    }
    
    public static int averageTemperature(int[] array){
        int i,average;
        int sum = 0;
        for(i = 0; i < array.length; i++){
            sum += array[i];
        }
        average = sum/array.length;
        return average;
    }
    public static int hottestTemperature(int[] array){
        int i;
        int max = array[0];
        
        for(i = 0; i < array.length; i++){
            if(array[i] > max)
                max = array[i];
        }
        return max;
    }
    public static int coldestTemperature(int[] array){
        int i;
        int min = array[0];
        
        for(i = 0; i < array.length; i++){
            if(array[i] < min)
                min = array[i];
        }
        return min;
    }
    public static int[] searchTemp(int[] temp, int key){
        int i;
        int n = 0;
        int[] day = new int[temp.length];
        
        for(i = 0; i < temp.length; i++){
            if(temp[i] == key){
                day[n] = i;
                n++;
            }
        }
        
        return day;
    }
}
Viewed 1419 times, submitted by Streusel.