호그와트

어이 김 씨, 한국은 춥댜 ~

영웅*^%&$ 2023. 11. 14. 07:13
728x90

import java.util.Scanner;

public class MonthlyTemperature {
    private static final int WEEKS = 4;
    private static final int DAYS_IN_WEEK = 7;
    private static double[][] temperatures = new double[WEEKS][DAYS_IN_WEEK];
    private static boolean dataEntered = false;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int choice;

        do {
            displayMenu();
            while (!scanner.hasNextInt()) {
                System.out.println("Please enter a valid number.");
                scanner.next(); // to ignore incorrect input
            }
            choice = scanner.nextInt();

            switch (choice) {
                case 1:
                    enterTemperatures(scanner);
                    break;
                case 2:
                    displayAllTemperatures();
                    break;
                case 3:
                    displayWeekTemperatures(scanner);
                    break;
                case 4:
                    displayDayTemperatures(scanner);
                    break;
                case 5:
                    displayLowestTemperature();
                    break;
                case 6:
                    displayHighestTemperature();
                    break;
                case 7:
                    displayAverageTemperature();
                    break;
                case 8:
                    System.out.println("Exiting program...");
                    break;
                default:
                    System.out.println("Please enter a number between 1 and 8.");
            }
        } while (choice != 8);

        scanner.close();
    }

    private static void displayMenu() {
        System.out.println("\nMenu:");
        System.out.println("1. Enter temperatures");
        System.out.println("2. Display all temperatures");
        System.out.println("3. Display temperatures of a specific week");
        System.out.println("4. Display temperatures of a specific day for all weeks");
        System.out.println("5. Display lowest temperature of the month");
        System.out.println("6. Display highest temperature of the month");
        System.out.println("7. Display average temperature of the month");
        System.out.println("8. Exit");
        System.out.print("Enter your choice: ");
    }

    private static void enterTemperatures(Scanner scanner) {
        for (int week = 0; week < WEEKS; week++) {
            for (int day = 0; day < DAYS_IN_WEEK; day++) {
                System.out.printf("Enter temperature for week %d, day %d: ", week + 1, day + 1);
                while (!scanner.hasNextDouble()) {
                    System.out.println("Please enter a valid temperature.");
                    scanner.next(); // to ignore incorrect input
                }
                temperatures[week][day] = scanner.nextDouble();
            }
        }
        dataEntered = true;
    }

    private static void displayAllTemperatures() {
        if (!dataEntered) {
            System.out.println("No temperature data available.");
            return;
        }
        for (int week = 0; week < WEEKS; week++) {
            System.out.println("Week " + (week + 1) + ":");
            for (int day = 0; day < DAYS_IN_WEEK; day++) {
                System.out.printf("Day %d: %.2f\n", day + 1, temperatures[week][day]);
            }
        }
    }

    private static void displayWeekTemperatures(Scanner scanner) {
        if (!dataEntered) {
            System.out.println("No temperature data available.");
            return;
        }
        System.out.print("Enter the week number (1-4): ");
        int week = getIntInRange(scanner, 1, 4) - 1;
        System.out.println("Temperatures for Week " + (week + 1) + ":");
        for (int day = 0; day < DAYS_IN_WEEK; day++) {
            System.out.printf("Day %d: %.2f\n", day + 1, temperatures[week][day]);
        }
    }

    private static void displayDayTemperatures(Scanner scanner) {
        if (!dataEntered) {
            System.out.println("No temperature data available.");
            return;
        }
        System.out.print("Enter the day number (1-7): ");
        int day = getIntInRange(scanner, 1, 7) - 1;
        System.out.println("Temperatures for Day " + (day + 1) + " of all weeks:");
        for (int week = 0; week < WEEKS; week++) {
            System.out.printf("Week %d: %.2f\n", week + 1, temperatures[week][day]);
        }
    }

    private static void displayLowestTemperature() {
        if (!dataEntered) {
            System.out.println("No temperature data available.");
            return;
        }
        double lowest = temperatures[0][0];
        for (int week = 0; week < WEEKS; week++) {
            for (int day = 0; day < DAYS_IN_WEEK; day++) {
                if (temperatures[week][day] < lowest) {
                    lowest = temperatures[week][day];
                }
            }
        }
        System.out.println("Lowest temperature of the month: " + lowest);
    }

    private static void displayHighestTemperature() {
        if (!dataEntered) {
            System.out.println("No temperature data available.");
            return;
        }
        double highest = temperatures[0][0];
        for (int week = 0; week < WEEKS; week++) {
            for (int day = 0; day < DAYS_IN_WEEK; day++) {
                if (temperatures[week][day] > highest) {
                    highest = temperatures[week][day];
                }
            }
        }
        System.out.println("Highest temperature of the month: " + highest);
    }

    private static void displayAverageTemperature() {
        if (!dataEntered) {
            System.out.println("No temperature data available.");
            return;
        }
        double total = 0;
        for (int week = 0; week < WEEKS; week++) {
            for (int day = 0; day < DAYS_IN_WEEK; day++) {
                total += temperatures[week][day];
            }
        }
        double average = total / (WEEKS * DAYS_IN_WEEK);
        System.out.println("Average temperature of the month: " + average);
    }

    private static int getIntInRange(Scanner scanner, int min, int max) {
        int number;
        do {
            while (!scanner.hasNextInt()) {
                System.out.println("Please enter a valid number.");
                scanner.next(); // to ignore incorrect input
            }
            number = scanner.nextInt();
            if (number < min || number > max) {
                System.out.printf("Please enter a number between %d and %d.\n", min, max);
            }
        } while (number < min || number > max);
        return number;
    }
}
728x90

'호그와트' 카테고리의 다른 글

panda's attack  (0) 2023.11.17
TwoArray.java  (0) 2023.11.14
실제의 해킹을 잘하고 싶다면...  (0) 2023.11.12
치킨버거 맛 좀 볼래요?  (0) 2023.11.09
leetcode sumArray  (0) 2023.11.05