统计正负数个数计算和和平均值

import java.util.*;
import javax.swing.*;  
  class Main {
    public static void main(String[] args){
      Scanner input = new Scanner(System.in);
      System.out.print("Enter an int value, the program exits if the input is 0: ");
      int number;
      int sum = 0;
      float avg = 0.0f;
      int countOfPositives = 0;
      int countOfNegatives = 0;
      do{
        number = input.nextInt();  
        
        if(number > 0)
          countOfPositives++;
        else if(number < 0)
          countOfNegatives++;

        sum += number;
      }while(number != 0);

      avg = (float)sum / (countOfNegatives+countOfPositives);
      System.out.print("The number of positives is " + countOfPositives);
      System.out.print("\nThe number of negatives is " + countOfNegatives);
      System.out.print("\nThe total is " + sum);
      System.out.print("\nThe average is " + avg);
    }
}
posted @ 2022-05-20 17:10  Scenery_Shelley  阅读(115)  评论(0编辑  收藏  举报