Hey there! First-time visitor? Contact Us About

Write a Java program to calculate max and min of temperature over 12 months, with below requirement.

Write a program to calculate the max and min of temperature over 12 months, with the below requirement.
1. Asks the user to enter the temperature for each month of the year.
2. For each input make sure the value must be between -15 and 50.
3. If the user enters an invalid value ask the user to enter again
4. Finally, the program displays only the maximum and minimum temperatures along with their corresponding Months' information.

The program should use Arrays and Loops to achieve the task.

Ans: 

import java.util.Scanner;

public class Temperature {
    public static void main(String[] args) {
        double[] temperature = new double[12];
        String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
        Scanner scanner = new Scanner(System.in);

        for (int i = 0; i < 12; i++) {
            System.out.print("Enter the temperature for " + months[i] + ": ");
            double temp = scanner.nextDouble();
            while (temp < -15 || temp > 50) {
                System.out.print("Invalid temperature. Please enter a temperature between -15 and 50: ");
                temp = scanner.nextDouble();
            }
            temperature[i] = temp;
        }

        double max = temperature[0];
        double min = temperature[0];
        int maxMonth = 0;
        int minMonth = 0;

        for (int i = 1; i < 12; i++) {
            if (temperature[i] > max) {
                max = temperature[i];
                maxMonth = i;
            }
            if (temperature[i] < min) {
                min = temperature[i];
                minMonth = i;
            }
        }

        System.out.println("Maximum temperature: " + max + " in " + months[maxMonth]);
        System.out.println("Minimum temperature: " + min + " in " + months[minMonth]);
    }
}

This program uses an array called temperature to store the temperature for each month of the year, and an array called months to store the names of the months. The program uses a for loop to repeatedly ask the user for the temperature for each month, and within the loop it uses a while loop to ensure that the temperature entered is between -15 and 50. If the user enters an invalid temperature, the program prompts the user to enter a new temperature. Once the user has entered the temperature for all 12 months, the program uses another for loop to find the maximum and minimum temperature in the temperature array, and also finds their corresponding month using the months array, and then prints out the maximum and minimum temperature along with their corresponding month.

About the Author

Experienced YouTuber with a demonstrated history of working in the Information Technology industry. Strong media and communication professional with a BCA focused in Science, Technology, and Society from Purbanchal University. Skilled in Graphics de…

إرسال تعليق

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.