0

I am trying to develop a program that calculates the sea level all the way up to the year 2100. Based on research I have found that the sea level will be 2.5 feet(which is 30 inches) - 6.5 feet(which is 78 inches). My program asks the user to enter which year they want to calculate how many inches the sea level has risen but I want the information to be random numbers per year between .3488 and .9069 inches because those are the averages per year that will make the sea level 2.5 feet and 6.5 feet. So my question is how can i generate random numbers for each year the user inputs so I can calculate how much the sea level has risen and then output that in the number of gallons.

  /takes the input from the user of all the way up to which year 
  /they want to know the number of inches/ gallons the sea level has risen
  /using reiman summs and a chart
  /
  /
  /*******************************************************************************/
 import java.util.Random;
 import java.util.Scanner;

  public class CalcProject
  {

  public static void main(String[] args)
  {
  //variables for the program
  int year;
  int numOfYears;
  double leastRise = .3488;  //y = .3488x , x is the inches of rainfall
  double highRise = .9069;   //y= .9069x , x is the inches of rainfall
  double seaLevel1;
  double seaLevel2;


  //Describing what the program does to the user
  System.out.println("This program caluclates the number of gallons / inches" +
                     "the sea level will rise based on your input." + "\n");

  //Asking for the users year they want to calculate
  System.out.println("Please enter which year you wish to know how much the water level will have            risen since 2014");

  Scanner scan = new Scanner(System.in);
  year = scan.nextInt();

  if(year <= 2014 || year > 2100)     
  {
    do
     {
      System.out.println("Please enter a valid year between 2015 and 2100");
      year = scan.nextInt();

     } while((year <= 2014 || year > 2100));

   }

   //puts the year into a number of years from 2014
   numOfYears = year-2014;
   System.out.println("The number of years between now and the year you chose is: " +                       numOfYears);

   //uses the least inches of sealevel
   seaLevel1 = numOfYears * leastRise;
   System.out.println("The total sealevel in inches is " + seaLevel1 + " for " + year);

   //uses the most inches of seaLevel
   seaLevel2 = numOfYears * highRise;
   System.out.println("The total sealevel in inches is " + seaLevel2 + " for " + year);

   //calculating radnom numbers for each year 
gainz
  • 1

1 Answers1

0

Here is a simple way of handling the weird random numbers.

int numOfYears;
double leastRise = .3488;
double highRise = .9069;
double seaLevel3 = 0;

int i = 0;
while (i < numOfYears) {
    double rand = Math.random();
    if (rand >= leastRise && rand <= highRise) {
        // rand is some sea level value representing a year
        seaLevel3 += rand;
        i++;
    }
}
System.out.println("The total sealevel in inches is " + seaLevel3 + " for " + year);
La-comadreja
  • 5,627
  • 11
  • 36
  • 64
  • Wow thank you so much! This is I guess a side question and I'm not sure if you are allowed t o do this but is there any way I can involve calculus in this program such as the area under a curve or maybe a sequence of some type? – gainz Dec 10 '14 at 04:35
  • @jay there should be. Check the Math library. This is a very vague question so we'd probably help you more if you're more specific about what you want to do. – La-comadreja Dec 10 '14 at 15:22