117k views
1 vote
Write a program that reads an unspecified number of integers from the user, determines how many positive and negative values have been read, computes the total and average of the input values (not counting zeros). Your program ends with the input 0. Display the average as a floating-point with 2 digits after the decimal. Here is an example of sample run: Enter an integer, the input ends if it is 0: 1 Enter an integer, the input ends if it is 0: 2 Enter an integer, the input ends if it is 0: -1 Enter an integer, the input ends if it is 0: 3 Enter an integer, the input ends if it is 0: 0 The number of positivies is 3 The number of negatives is 1 The total is 5 The average is 1.25

User Unexist
by
4.3k points

1 Answer

2 votes

Answer:

see explaination

Step-by-step explanation:

import java.util.Scanner;

public class NumbersStats {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

int pos = 0, neg = 0, count = 0, num;

double total = 0;

System.out.print("Enter an integer, the input ends if it is 0: ");

while (true) {

num = in.nextInt();

if(num == 0) break;

if(num > 0) {

pos++;

} else {

neg++;

}

total += num;

count++;

}

System.out.println("The number of positives is " + pos);

System.out.println("The number of negatives is " + neg);

System.out.println("The total is " + total);

System.out.println("The average is " + (total/count));

}

}

User Ice
by
4.3k points