Java Problem Avg Rainfall?
-
I need help writing a program using nested loops. Im trying to create a program that uses nested loops to collect data and calculate the average rainfall over a period of years. First the program should ask for the number of years. The outer loop will iterate once for each year. The inner loop will interate 12 times, once for each month. Each iteration of the inner loop will ask the user for the inches of rainfall for that month. After all interations, the program needs to display the number of months, the total inches of rainfall, and the average rainfall per month for the entire period using JOptionPane. The problem I'm having is the >0 or <1 verfication, and calculation. I think i'm doing something completely wrong :( import javax.swing.JOptionPane; // import GUI class import java.text.DecimalFormat; public class avgRain { /** * Rob Thompson * Assignment 5 * pg 243 #8 - Average Rainfall */ public static void main(String[] args) { // Declare Variables int rainfall; int year; int total=0; double average=0; String input; // input - Years and Inches of Rainfall // output - avg rainfall per month // Processing - d = s * t // Ask user to enter years do { input = JOptionPane.showInputDialog("How many years to calculate?"); year=Integer.parseInt(input); } // while year <=1 error msg and loop back while(year>=1); { input = JOptionPane.showInputDialog("Please enter a number greater than one"); year=Integer.parseInt(input); } for(int i = 0;i<year;i++) { for (int month = 1; month <= 12; month++) { input = JOptionPane.showInputDialog("Enter the inches of rainfall for month " +month); rainfall=Integer.parseInt(input); // while rainfall >0 error msg and loop back while (rainfall>0) { input = JOptionPane.showInputDialog("Please enter a number greater than zero" +month); rainfall=Integer.parseInt(input); average = Double.parseDouble(input); average=((double)total) / 12.0; System.out.println("The total for year # "+ i + " is " + total ); System.out.println("The average for year # "+ i + " is " + (average )); } } } } }
-
Answer:
do { input = JOptionPane.showInputDialog("How many years to calculate?"); year=Integer.parseInt(input); }while(year<1); double gtotal=0; for(int i = 0;i<year;i++) { double total=0; for (int month = 1; month <= 12; month++) { do { input = JOptionPane.showInputDialog("Enter the inches of rainfall for month " +month); rainfall=Integer.parseInt(input); }while (rainfall<0); total +=rainfall; gtotal +=rainfal; } average=((double)total) / 12.0; System.out.println("The total for year # "+ i + " is " + total ); System.out.println("The average for year # "+ i + " is " + (average )); } } System.out.println("Average rainfall for all years=" + (gtotal/(12*years)) );
Sdgsdgsdg Sdgsdg at Yahoo! Answers Visit the source
Other answers
do { input = JOptionPane.showInputDialog("How many years to calculate?"); year=Integer.parseInt(input); }while(year<1); double gtotal=0; for(int i = 0;i<year;i++) { double total=0; for (int month = 1; month <= 12; month++) { do { input = JOptionPane.showInputDialog("Enter the inches of rainfall for month " +month); rainfall=Integer.parseInt(input); }while (rainfall<0); total +=rainfall; gtotal +=rainfal; } average=((double)total) / 12.0; System.out.println("The total for year # "+ i + " is " + total ); System.out.println("The average for year # "+ i + " is " + (average )); } } System.out.println("Average rainfall for all years=" + (gtotal/(12*years)) );
James Bond
Related Q & A:
- Whats the avg rent price in Milano?Best solution by Yahoo! Answers
- What is the average rainfall of French Guiana?Best solution by weatherspark.com
- How do you disable AVG Antivirus?Best solution by Yahoo! Answers
- How do I uninstall the AVG free anti virus 2011 toolbar?Best solution by Yahoo! Answers
- Why am I having this problem with java or Windows Installer?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.