Results 1 to 4 of 4
  1. #1
    Raiderofrice is offline Member
    Join Date
    Oct 2009
    Posts
    2
    Rep Power
    0

    Default 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

  2. #2
    jefrazie is offline Member
    Join Date
    Oct 2009
    Posts
    7
    Rep Power
    0

    Default

    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. #3
    Raiderofrice is offline Member
    Join Date
    Oct 2009
    Posts
    2
    Rep Power
    0

    Default

    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);      
        }
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Execute A program from a Program!
    By Moncleared in forum Advanced Java
    Replies: 2
    Last Post: 02-22-2009, 04:17 PM
  2. Executing a program within a program
    By gibsonrocker800 in forum New To Java
    Replies: 5
    Last Post: 05-12-2008, 08:24 AM
  3. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  4. Replies: 0
    Last Post: 10-04-2007, 09:33 PM

Posting Permissions

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