HW4.1

时间:2023-03-10 02:35:32
HW4.1

HW4.1

 import java.util.Scanner;

 public class Solution
 {
     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 inputValue = input.nextInt();
         int count = 1;

         int positive = 0;
         int negative = 0;
         if(inputValue > 0)
             positive = 1;
         else if(inputValue < 0)
             negative = 1;

         int sum = inputValue;

         while(inputValue != 0)
         {
             System.out.print("Enter an int value, the program exits if the input is 0: ");
             inputValue = input.nextInt();
             count++;
             sum += inputValue;
             if(inputValue > 0)
                 positive++;
             else if(inputValue < 0)
                 negative++;
         }

         float average = (float)(sum * 1.0) / count;

         System.out.println("The number of positives is " + positive);
         System.out.println("The number of negatives is " + negative);
         System.out.println("The total is " + count);
         System.out.println("The average is " + average);
     }
 }