Advertisement
eimkasp

Java array averate

Apr 18th, 2019
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.67 KB | None | 0 0
  1. import java.text.DecimalFormat;
  2. import java.util.Scanner;
  3.  
  4.  
  5. public class Main {
  6.  
  7.     public static void main(String[] args) {
  8.  
  9.         int[] a = {87, 6, 11, 15, 8, 4};
  10.         // 87 87.0
  11.         int suma = 0;
  12.         double vidurkis;
  13.        
  14.  
  15.  
  16.         /* skaiciuojame masyvo elementu suma*/
  17.         for(int i = 0; i < a.length; i++) {
  18.             suma += a[i];
  19.         }
  20.        
  21.         /* skaiciuojame vidurki */
  22.         vidurkis = (double)suma / (double)a.length;
  23.        
  24.         /* apvaliname gauta rezultata */
  25.         DecimalFormat df = new DecimalFormat("#.000");
  26.         String rez = df.format(vidurkis);
  27.  
  28.         System.out.println(rez);
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement