Results 1 to 4 of 4
Thread: SumAndAverage Program Help!
- 10-04-2009, 10:32 PM #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: "); 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); } }Last edited by Raiderofrice; 10-05-2009 at 04:07 AM. Reason: code tags added
- 10-05-2009, 01:52 AM #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:
I have tested that and it works.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); } }
- 10-05-2009, 03:46 AM #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: "); 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); } }
-
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.
Similar Threads
-
Execute A program from a Program!
By Moncleared in forum Advanced JavaReplies: 2Last Post: 02-22-2009, 04:17 PM -
Executing a program within a program
By gibsonrocker800 in forum New To JavaReplies: 5Last Post: 05-12-2008, 08:24 AM -
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:40 PM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:33 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks