1. Member
Join Date
Oct 2009
Posts
2
Rep Power
0

## 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”.

Java 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: ");

while (number != 0) {
System.out.print("Enter number: ");
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);

}
}```
Last edited by Raiderofrice; 10-05-2009 at 04:07 AM. Reason: code tags added

2. Member
Join Date
Oct 2009
Posts
7
Rep Power
0
Hey here is what i came up with when i went through your code:
Java 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.

3. Member
Join Date
Oct 2009
Posts
2
Rep Power
0
Thank you very much for the input! It helped me figure out a way to solve it.

Java 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: ");
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);
}
}```

4. 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.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•