programmingex5 1 java

Solutions on MaxInterview for programmingex5 1 java by the best coders in the world

showing results for - "programmingex5 1 java"
Camilo
25 Jul 2018
1import java.util.Scanner;
2 
3public class ProgrammingEx5_1 {
4 
5 public static void main(String[] args) {
6  Scanner input = new Scanner(System.in);
7  System.out.print("Enter an integer, the input ends if it is 0:");
8  int n, countNeg = 0, countPos = 0;
9  float sum = 0;
10 
11  while ((n = input.nextInt()) != 0) {
12   sum = sum + n;
13 
14   if (n > 0) {
15    countPos++;
16   } else if (n < 0) {
17    countNeg++;
18   }
19 
20  }
21 
22  if (countPos + countNeg == 0) {
23   System.out.println("No numbers are entered except 0");
24   System.exit(0);
25  }
26 
27  System.out.println("The number of positives is " + countPos);
28  System.out.println("The number of negatives is " + countNeg);
29  System.out.println("The total is " + sum);
30  System.out.println("The average is " + (sum / (countPos + countNeg)));
31 
32 }
33 
34}
35
similar questions
queries leading to this page
programmingex5 1 java