samedi 5 novembre 2016

Is there a way to use the existing variables in a random array method in another method?

I want to use the variables generated in my randomRainMonth() method to be used for calculation in another method totalRain(). But by using double[] totalsum = randomRainMonth(list); in my totalRain() it would just generate a new set of random variables as it would invoke the method again. Can anyone help?

public class RainFall{

  public static void main (String[] args){

    double [] monthRain = new double[12];
    String[] monthName = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
    randomRainMonth(monthRain);
    System.out.println("-------");
    totalRain(monthRain, monthName);
//    printMonths(monthName);
  }

  public static String[] printMonths(String list[]){     
    String[] pM = new String[list.length];  
    for (int i = 0; i<list.length; i++) {
        //System.out.println(list[i]);
      }
    return pM;
  }

    public static double[] randomRainMonth(double list[]){
      double[] rdMonth = new double[list.length];

      for(int i = 0; i < list.length; i++){
        rdMonth[i] = (int)(Math.random() * 100);
        //System.out.println(rdMonth[i]);
      }
      return rdMonth;
  }

    public static double[] totalRain(double list[], String list2 []){

      String[] mnth = printMonths(list2);
      double[] totalsum = randomRainMonth(list);

      double sum = 0;
      for( double i : totalsum) {
        sum += i;
      }


      for (int i = 0; i<list2.length; i++) {

      System.out.print(mnth[i] +": ");
      System.out.println(totalsum[i]);
      }
      System.out.println("Total: ");
      System.out.println(sum);

      return totalsum;

    }
}




Aucun commentaire:

Enregistrer un commentaire