Quick actions

cmd+k|ctrl+k

Navigation

Languages

Arrays - Getting Inputs from User with implemented avrg-method

Snippet info

Language

Java

Visibility

public

Author

orochi-100

Created

2020-04-06T08:51:53Z

Updated

2020-04-06T08:51:53Z

import java.util.Scanner;
//Unfinished -> Need  some  more functions  implementations 


class Main {
    
    
    public static Scanner scanner  = new Scanner(System.in);
    
    
    
    public static void main(String[] args) {
       
       int[] arrayIntegers = getIntegers(6);
       
       for(int i=0; i < arrayIntegers.length; i++)
       {
           System.out.println("Element " + i  + " Typed value was " + arrayIntegers[i]);
       }
      System.out.println("Average is " + getAverage(arrayIntegers));
   
   
    }
    
    
    /**
     * Get Integers  -> this method takes one parameter, this parameter is the size of the Array
     * and we place the Integer Number as the array size -> int[] values =  new int[numb];
     * and we  say -> go through this array until his size and since the end of array is not reached ask for a next Integer Input
     */
    public static int[] getIntegers(int numb) 
    {
       System.out.println("Please enter "  + numb + " integer values on the array : \n");
        
        
        //The number now gonna define the array size
        int[] values =  new int[numb];
        
        
        //since the end of  array is not reached we ask for new input otherwise we break it.
        for(int i=0; i < values.length; i++)
        {
            values[i] = scanner.nextInt();
        }
        
        //Return the entire array  
        return values;
    }
    
    /**
     * Get the Average
     */
    public static double getAverage(int[] array)
    {
        int sum = 0;
        for (int number : array) sum += number;
        return (double) sum / array.length;
    }
    
    
    
}
INFO