Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Rainfall program not calculating correctly

The program is suppose to prompt the user for number of years. Entry must be greater than 0 to continue. If not greater than 0 keep re-asking the prompt. (This works)

Then the program is to ask the user for the rainfall in inches for each month through all twelve months of each year. (Working) If the number entered is a negative that specific prompt should be re-asked. (not-working)

Lastly, after all data is entered the total number of months should be display. (not working correctly) The total rainfall amount should display. (not working correctly) The average of rainfall per month for the entire period should display. (working)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import java.util.Scanner;
import javax.swing.JOptionPane;

public class AverageRainfall {
    public static void main(String[] args) {
        int num_of_years = 0;
        int total_num_of_months = 0;
        int NUM_OF_MONTHS = 12;
        double rainfall = 0;
        double total_rainfall = 0;
        double average_rainfall_per_month = 0;
        String input;

        input = JOptionPane.showInputDialog("Enter number of years: ");
        num_of_years = Integer.parseInt(input);

        while (num_of_years < 1){
            input = JOptionPane.showInputDialog("The number of years must be an integer greater than 1");
            num_of_years = Integer.parseInt(input);
        }
        for (int i = 0; i < num_of_years; i++){
            for(int j = 0; j < NUM_OF_MONTHS; j++)
            input = JOptionPane.showInputDialog("In inches, how much rainfall was there for month " + (j + 1) + ":");
            rainfall = Double.parseDouble(input);
            total_rainfall += rainfall;
            total_num_of_months++;
        }
        average_rainfall_per_month = total_rainfall / total_num_of_months;
        System.out.println("Total number of months: " + total_num_of_months);
        System.out.println("Total inches of rainfall: " + total_rainfall);
        System.out.println("Average rainfall per month = " + average_rainfall_per_month);
    }
}

>Solution :

First, the code block below the inner for loop (for(int j = 0; j < NUM_OF_MONTHS; j++)) should be wrapped between a pair of curly brackets ({}).

Next, I don’t see any checks for non-negative rainfall. You could do something similar to what you’ve already done. So that entire part would look something like this:

    for (int i = 0; i < num_of_years; i++){
        for(int j = 0; j < NUM_OF_MONTHS; j++){
            input = JOptionPane.showInputDialog("In inches, how much rainfall was there for month " + (j + 1) + ":");
            rainfall = Double.parseDouble(input);
            while (rainfall < 0){
                input = JOptionPane.showInputDialog("The amount of rainfall must be non-negative");
                rainfall = Double.parseDouble(input);
            }
            total_rainfall += rainfall;
            total_num_of_months++;
        }
    }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading