# SumAndAverage Program Help!

• 10-04-2009, 10:32 PM
Raiderofrice
SumAndAverage Program Help!
Solved! Answer is down below if anyone is interested.
----------------
Almost have it figured out... However, every time the program loops, I think the sum and average get reset to sum=0 and average=1, and I cannot figure out a way around it. Any help would be greatly appreciated!
--------------------------------------…
Write a program that reads an unspecified number of integers from the keyboard. When the user enters a ‘0’ this signifies the end of data entry and doesn’t itself count as one of the entered values. The program must compute the sum of all the numbers and the average value of the numbers. The program must be named “SumAndAverage”.

Code:

```public class SumAndAverage { public static void main(String [] args) { int number; int sum=0; int average=1; int count = 1; System.out.print("Enter number: "); number = Keyboard.readInt(); while (number != 0) { System.out.print("Enter number: "); number = Keyboard.readInt(); sum = sum + number; average = sum / count; count ++; } count = count -1; System.out.println("The sum of these " + count + " numbers is: " + sum); System.out.println("The average of these " + count +" numbers is: " + average); } }```
• 10-05-2009, 01:52 AM
jefrazie
Hey here is what i came up with when i went through your code:
Code:

```import java.util.Scanner; public class SumAndAverage {         public static void main(String [] args)         {                 int number = 1;                 int sum = 0;                 int average = 0;                 int count = -1;                                 do                 {                         System.out.print("Enter number: ");                         Scanner input = new Scanner(System.in);                         number = input.nextInt();                         sum = sum + number;                         count ++;                 }                 while (number != 0);                                 average = sum / count;                 System.out.println("The sum of these " + count + " numbers is: " + sum);                 System.out.println("The average of these " + count +" numbers is: " + average);         } }```
I have tested that and it works.
• 10-05-2009, 03:46 AM
Raiderofrice
Thank you very much for the input! It helped me figure out a way to solve it.

Code:

```public class SumAndAverage {     public static void main(String [] args) { // Object declaration         int number = 1;         int sum = 0;         int average = 0;         int count = 0;         // Indefinite while loop         while (number != 0) {             count++;             System.out.print("Enter number: ");             number = Keyboard.readInt();             sum = sum + number;             average = sum / count;         }         // Results         count = count - 1;         average = sum / count;         System.out.println("The sum of these " + count + " numbers is: " + sum);         System.out.println("The average of these " + count +" numbers is: " + average + " and " + sum%count + " / " + count);          } }```
• 10-05-2009, 04:01 AM
Fubarable
I'm glad that you've solved it and glad that you marked it solved in your cross-post. Best of luck in your Java studies.