Please help with quick program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tiredguy106
    New Member
    • Mar 2007
    • 1

    #1

    Please help with quick program

    I have a program that I have for homework and I tried to figure it out, but I have spent a lot of time on it. Please help me. The problem i'm having is that it is including the 0 in the sum, product and count. I'm not sure what to do. Anyway here is the code...
    By the way, thanks. (I tried to add comments to help you understand what I have some things there for)
    Code:
    import java.util.*;
    
    class Compute 
    {
        public static void main(String[] args) 
        {
    
            Scanner scanner = new Scanner(System.in);
            
            int count = 0;    //counts numbers to create average later
            int rSum = 0;     //running sum
            int rProd = 1;    //running product
            int number = 1;   //initializes variable
            double average;   //average of numbers input
            
            System.out.println("Enter numbers. Use 0 to end input");
            while (number != 0)
            {
                number = scanner.nextInt();
                rSum = rSum + number;
                rProd = rProd * number;
                count++;
            }
            average = (double) rSum / count; //makes variables double
            System.out.println("Sum = " + rSum);
            System.out.println("Product = " + rProd);
            System.out.println("Average = " + average);
        }
    }
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Get the first input before you enter the loop, then do all your calculations first in the loop - the last statement in the loop will be the input from the Scanner.

    Comment

    Working...