grodek118

Average of positive numbers

Apr 17th, 2022 (edited)
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.99 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6.  
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.         int positiveCount = 0;
  10.         int positiveSum = 0;
  11.  
  12.         while (true) {
  13.             System.out.println("Give numbers:");
  14.             int number = Integer.valueOf(scanner.nextLine());
  15.  
  16.             if (number > 0) {
  17.                 positiveSum += number;
  18.                 positiveCount = positiveCount + 1;
  19.             } else if (number < 0) {
  20.                 continue;
  21.             } else {
  22.                 break;
  23.             }
  24.         }
  25.  
  26.         if (positiveCount == 0) {
  27.             System.out.println("Cannot calculate the average");
  28.             return;
  29.         }
  30.  
  31.         double average = positiveSum / positiveCount;
  32.  
  33.         System.out.println("positive " + positiveSum);
  34.         System.out.println("positive count " + positiveCount);
  35.         System.out.println("positive average " + average);
  36.     }
  37. }
Add Comment
Please, Sign In to add comment