I need help writing this program in Java. A simple, easy to understand program w
ID: 3937752 • Letter: I
Question
I need help writing this program in Java. A simple, easy to understand program would be appreciated:
Write a program that reads an unspecified number of integers, determines how many positive and negative values have been read, and 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 number. Here is a sample run:
Enter an integer, the input ends if it is 0: 1 2 -1 3 0
The number of positives is 3
The number of negatives is 1
The total is 5.0
The average is 1.25
Sample run #2:
Enter an integer, the input ends if it is 0: 0
No numbers are entered except 0
Explanation / Answer
CountPositiveNegativeNos.java
import java.util.ArrayList;
import java.util.Scanner;
public class CountPositiveNegativeNos {
public static void main(String[] args) {
// Declaring variables
int num, count_negative = 0, count_positive = 0, total_nos, flag = 0, count = 0;
double average = 0.0, sum = 0.0;
// Creating an ArraList Object
ArrayList ar = new ArrayList();
// Scanner class object is used to read the inputs entered by the user
Scanner sc = new Scanner(System.in);
/*
* This while loop continues to execute until the user enters zero as
* input
*/
while (true) {
// Getting the input entered by the user
System.out.print("Enter a integer,the input ends if it is zero :");
num = sc.nextInt();
/*
* checking If the input number is less than zero if yes,count the
* negative numbers
*/
if (num < 0) {
// Counting the negative numbers
count_negative++;
count++;
// Calculating the sum
sum += num;
continue;
}
/*
* checking If the input number is greater than zero if yes,count
* the positive numbers
*/
else if (num > 0) {
// Count positive numbers
count_positive++;
count++;
sum += num;
continue;
}
/*
* checking If the input number is equal to zero if yes,Display the
* message
*/
else if (num == 0) {
count++;
break;
}
}
if (count == 1) {
System.out.println("No numbers are entered except 0");
} else if (count > 1) {
// total positive and negative numbers
total_nos = count_negative + count_positive;
// Calculating the average
average = sum / total_nos;
// Displaying the count of positive numbers
System.out.println("The Number of Positives is " + count_positive);
// Displaying the count of negative numbers
System.out.println("The Number of Negatives is " + count_negative);
// Displaying the sum of positive numbers and negative numbers
System.out.println("The Total is " + sum);
// Displaying the average of all the numbers
System.out.println("The Average is " + average);
}
}
}
______________________________________
output1:
Enter a integer,the input ends if it is zero :1
Enter a integer,the input ends if it is zero :2
Enter a integer,the input ends if it is zero :-1
Enter a integer,the input ends if it is zero :3
Enter a integer,the input ends if it is zero :0
No numbers are entered except 0
The Number of Positives is 3
The Number of Negatives is 1
The Total is 5.0
The Average is 1.25
________________________________
Output2:
Enter a integer,the input ends if it is zero :0
No numbers are entered except 0
____________Thank You