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

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));

}

}

ijeggs

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

ACCESS MORE
EDU ACCESS
Universidad de Mexico