Write a program in java that reads an unspecified number of integers, determines
ID: 3638632 • Letter: W
Question
Write a program in java that reads an unspecified number of integers, determines how manypositive 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 double.
Example:
Enter an int value, the program exits if the input is 0.
User Enters: 51
Enter an int value, the program exits if the input is 0.
User Enters: -5
Enter an int value, the program exits if the input is 0.
User Enters: -8
Enter an int value, the program exits if the input is 0.
User Enters: 0
The number of positives is: 1
The number of negatives is: 2
The total is: 38
The average is 12.666
Explanation / Answer
please rate - thanks
import java.util.*;
public class main
{
public static void main(String [] args)
{ int n,pos=0,neg=0,total=0;
Scanner in=new Scanner(System.in);
System.out.print("Enter an int value, the program exits if the input is 0. ");
n=in.nextInt();
while(n!=0)
{total+=n;
if(n>0)
pos++;
else
neg++;
System.out.print("Enter an int value, the program exits if the input is 0.51 ");
n=in.nextInt();
}
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.printf("The average is: %.3f ",(double)total/(neg+pos));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.