I have to compute the average in this program, but the average cannot consists of zero which ends the program. For example if I enter 1, 2, 3, -1, then 0 to exit, the average will be the sum (5) over 4 and not 5 since the last number, the zero does not go in the sum. Any help?
Thanks.
public class Problddm1 { public static void main (String []args){ Scanner scan = new Scanner(System.in); // Object declaration int number = 1; int sum = 0; float average = 0; float count = 0; int posnum =0; int negnum =0; // While loop do{ count++; System.out.print("Enter a number, program exits on 0: "); number = scan.nextInt(); sum = sum + number; average = sum / count; if (number <0) { negnum++; } else if (number >0) { posnum++; } } while (number != 0); // Results System.out.println("The number of positives is: " +posnum); System.out.println("The number of negatives is: " +negnum); System.out.println("The total is: " + count); System.out.println("The average is: " +average); } }