Respuesta :
Answer:
see explaination
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));
}
}
Answer:
import java.util.Scanner;
public class num4 {
public static void main(String[] args) {
System.out.println("Enter an integer, the input ends if it is 0");
Scanner in = new Scanner(System.in);
int posCount = 0, negCount = 0, sum = 0;
int num = in.nextInt();
if(num < 0){
negCount++;
}
else if(num > 0){
posCount++;
}
sum +=num;
while (num != 0){
System.out.println("Enter an integer, the input ends if it is 0");
num = in.nextInt();
if(num < 0){
negCount++;
}
else if(num > 0){
posCount++;
}
sum+=num;
}
int total = sum;
double average = (double) total/(posCount+negCount);
System.out.println("The number of Positives is "+posCount);
System.out.println("The number of Negatives is "+negCount);
System.out.println("The Total is "+total);
System.out.printf("The average is %.2f ",average);
}
}
Explanation:
Solution is in Java
Use Scanner Class to accept and save user inputs
Create variables for sum, positiveCount and negativeCount
Use a while loop to continuously ask user to enter a number and terminate if number entered is 0
if number entered is not zero, then determine if it is negative or positive and increment the respective variable. Keep track of Sum(total)
Outside the while loop output the statements as given in the question and average to 2 decimal places