简体   繁体   中英

Need quick help on rainfall statistics program

basically I am having a pretty rough time on putting together this program that is designed to ask the users how many years of data they want to see, and based on that input, that is how many sets of 12 months of random double rainfall values are displayed.

This is what the output should look like if the user entered 3 years :

Enter the number of years: 3

Year 1 rainfall amounts were

Month 1 rainfall amount was 4.16 Month 2 rainfall amount was 6.85 Month 3 rainfall amount was 1.28 Month 4 rainfall amount was 1.66 Month 5 rainfall amount was 3.06 Month 6 rainfall amount was 4.62 Month 7 rainfall amount was 5.69 Month 8 rainfall amount was 1.84 Month 9 rainfall amount was 9.63 Month 10 rainfall amount was 8.60 Month 11 rainfall amount was 3.34 Month 12 rainfall amount was 1.27

Year 2 rainfall amounts were

Month 1 rainfall amount was 4.38 Month 2 rainfall amount was 4.64 Month 3 rainfall amount was 8.27 Month 4 rainfall amount was 3.19 Month 5 rainfall amount was 6.69 Month 6 rainfall amount was 4.13 Month 7 rainfall amount was 8.52 Month 8 rainfall amount was 5.71 Month 9 rainfall amount was 3.91 Month 10 rainfall amount was 9.16 Month 11 rainfall amount was 3.47 Month 12 rainfall amount was 7.90

Year 3 rainfall amounts were

Month 1 rainfall amount was 2.73 Month 2 rainfall amount was 2.92 Month 3 rainfall amount was 7.98 Month 4 rainfall amount was 2.82 Month 5 rainfall amount was 3.73 Month 6 rainfall amount was 3.00 Month 7 rainfall amount was 0.14 Month 8 rainfall amount was 9.98 Month 9 rainfall amount was 5.34 Month 10 rainfall amount was 8.12 Month 11 rainfall amount was 8.66 Month 12 rainfall amount was 4.21

Number of months: 36 Total rainfall: 181.62 inches Average monthly rainfall: 5.04 inches

What I have thus far is :

import java.util.*;


/**
  This class creates a program that finds the amount of rainfall for each year
*/
public class RainfallStats {
/**
  The main method is the program's starting point 
*/
public static void main(String[] args){

  int years = 0;
  double months = 0;
  final int numberMonths = 12;

  Scanner keyboard = new Scanner(System.in);
    Random generator = new Random();

  System.out.println("Enter the number of years: ");
  years = keyboard.nextInt();

  while (years <1){
     System.out.println("Invalid. Enter at least one year");
     years = keyboard.nextInt();
  }

  for (int y = 1; y <= years; y++){

      System.out.println("Year " + y + " rainfall amounts were");
      System.out.println();
  }    

     for (int m = 1; m <= numberMonths; m++){

        System.out.println("Month " + m + " rainfall amount was ");

I just need help in the loop and figuring out how to place a different random double value after each of the month statements. I know I probably have some things out of order and am missing quite a bit, but I would appreciate any hints or tips. Thanks!

Given a maximum amount of rainful maxRainfall , which would be a final variable, you can get a random decimal using the nextDouble function of java.util.Random , and multiplying it by your maxRainfall :

double monthRainfall = generator.nextDouble() * maxRainfall

Then you can do whatever you wish with that new random double, which will be between 0 and maxRainfall

To output the months after the appropriate year, instead of all the years then all the months, nest your for loop for the months inside the for loop for the years:

for (int y = 1; y <= years; y++)
{
    System.out.println("Year " + y + " rainfall amounts were");
    System.out.println();
    for (int m = 1; m <= numberMonths; m++)
    {
        System.out.println("Month " + m + " rainfall amount was ");
        //Code to generate random double here
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM