1 import java.util.Scanner; 2 3 public class Solution 4 { 5 public static void main(String[] args) 6 { 7 Scanner input = new Scanner(System.in); 8 9 System.out.print("Enter an int value, the program exits if the input is 0: "); 10 int inputValue = input.nextInt(); 11 int count = 1; 12 13 int positive = 0; 14 int negative = 0; 15 if(inputValue > 0) 16 positive = 1; 17 else if(inputValue < 0) 18 negative = 1; 19 20 int sum = inputValue; 21 22 while(inputValue != 0) 23 { 24 System.out.print("Enter an int value, the program exits if the input is 0: "); 25 inputValue = input.nextInt(); 26 count++; 27 sum += inputValue; 28 if(inputValue > 0) 29 positive++; 30 else if(inputValue < 0) 31 negative++; 32 } 33 34 float average = (float)(sum * 1.0) / count; 35 36 System.out.println("The number of positives is " + positive); 37 System.out.println("The number of negatives is " + negative); 38 System.out.println("The total is " + count); 39 System.out.println("The average is " + average); 40 } 41 }
时间: 2024-10-07 17:12:38