📜  不接收任何参数的静态方法 avg High Temp() - 无论代码示例

📅  最后修改于: 2022-03-11 15:00:10.908000             🧑  作者: Mango

代码示例1
import java.util.*;

public class Temperature {
    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);

        // Input the number of days from the user.
        System.out.print("How many days' temperatures? ");
        int days = console.nextInt( );

        // Declare and create an array, maybe should check if days is positive
        int[ ] temps = new int[days];

        // Input and store the temperatures in the array
        for (int i = 0; i < temps.length; i++) {
            System.out.print("Day " + i + "'s high temp: ");
            temps[i] = console.nextInt( );
        }

        // Calculate and print the average
        int sum = 0;
        for (int i = 0; i < temps.length; i++) {
            sum += temps[i];
        }
        // need a cast to avoid integer division
        double average = (double) sum / temps.length;
        System.out.println("Average temp = " + average);

        // Count the number of values that were above average
        int count = 0;
        for (int i = 0; i < temps.length; i++) {
            if (temps[i] > average) {
                count++;
            }
        }
        System.out.println(count + " days were above average");
    }
}