Advertisement
Flaron

Udemy_27. InputCalculator

Sep 14th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.  
  5. public static void inputThenPrintSumAndAverage () {
  6. Scanner scanner = new Scanner(System.in);
  7. int count=0;
  8. int sum=0;
  9. long avg;
  10. while (true) {
  11. if (scanner.hasNextInt()) {
  12. int num = scanner.nextInt();
  13. sum += num;
  14. count++;
  15. } else {
  16. avg = (int)Math.round((double)sum / (double)count);
  17. System.out.println("SUM = " + sum + " AVG = " + avg);
  18. break;
  19. }
  20. }
  21. }
  22.  
  23. public static void main(String[] args) {
  24. inputThenPrintSumAndAverage();
  25. }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement