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