Advertisement
Stelios_Gakis

Exercise_5/Task_4

Oct 4th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.17 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3.  
  4. public class Task_4 {
  5.  
  6.     private static final Scanner in = new Scanner(System.in);
  7.  
  8.     public static void main(String[] args) {
  9.  
  10.         ArrayList<Integer> numbers = new ArrayList<Integer>();
  11.         boolean exit = false;
  12.         while (!exit) {
  13.             System.out.println("1. Enter new number\n2. Exit");
  14.             int input = in.nextInt();
  15.             if (input == 1) {
  16.                 System.out.printf("Number : ");
  17.                 numbers.add(in.nextInt());
  18.             } else if (input == 2) {
  19.                 exit = true;
  20.             } else {
  21.                 System.out.println("Enter a proper answer.");
  22.             }
  23.         }
  24.  
  25.         int min = 999*999*999;
  26.         int max = -1;
  27.         double total = 0;
  28.         for (int element: numbers) {
  29.             if (element < min) {
  30.                 min = element;
  31.             }
  32.             if (element > max) {
  33.                 max = element;
  34.             }
  35.             total += element;
  36.         }
  37.         double average = total / numbers.size();
  38.         System.out.printf("Min : %d%nMax : %d%nAverage : %.2f", min, max, average);
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement