vendredi 26 novembre 2021

How to get the value from each index?

I'm extremely new to Java and we're tasked to take random values of an array and pass them through a method where it adds all of them for a running total.

For the sumMethod I'd like to take each value from all the index (given by sizeOfArray) and add them together.

Thank you!

public static void sumMethod(double[] arrayOfDoubles){
   //How to get the value from each indexes (given by sizeOfArray) and add them for the sum
   int arrayLength = arrayOfDoubles.length;
         
        System.out.println(arrayOfDoubles);
   }

  public static void main(String[] args) {   
   //1-3: Set up Scanner object to collect user's input on the size of array.
      Scanner keyboard = new Scanner(System.in);
      System.out.println("How many double numerical entries do you have?");
   //4: Declare an array of size sizeOfArray
      int sizeOfArray = keyboard.nextInt();  
   
      //Initialize array
      double[] arrayOfDoubles;
      arrayOfDoubles = new double[sizeOfArray];   
   
      
      
   
      for(int i = 0; i < sizeOfArray; i++){
      //5: Use the Random number Class and walk over the array 
         Random randomNum = new Random();       
         arrayOfDoubles[i] = randomNum.nextDouble(0.0 , 100.0);
      //6: Invoke SumMethod
         sumMethod(arrayOfDoubles); 
      }
      
     
      
      
   }
}



Aucun commentaire:

Enregistrer un commentaire