Reputation: 1
I'm having trouble on another Java program, and it's probably something really small, but for some reason, I can't understand what the issue is. Similar questions have been brought up many times before, but it doesn't give the exact same thing needed for this program, at least the answers being given didn't help.
The program needs to calculate the average of the amount set of numbers that the user initially inputs. Which is declared as inputNumber
. The program then asks the user for each number of the set amount the user specified. This is addedNumber
, and all of them are added to a total, which is numberSum
, and is then divided by inputNumber
to get the total's average, which value is declared as numberAverage
.
For example: If the program prompts me for how many numbers I want to input and I type 4 (inputNumber
), the scanner prompts me for a number 4 times (addedNumber
), and I type in 4 numbers, such as the numbers: 2, 3, 6, and 1. Those numbers are added together as the sum 12 (numberSum
), and then divided to get an average of the 4 numbers (numberAverage
).
The counter i
that will calculate how many times to prompt the user will increase until it reaches the number that the user inputted to have calculated is declared separately in the for
loop. And the whole program happens on a while
loop until the user inputs 0
as the value of inputNumber
, which will then break the loop and terminate the program.
Here's the code:
import java.util.Scanner;
/**
* Write a description of class AverageNumbers here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Average2
{
public static void main ()
{
int inputNumber;
double numberSum = 0;
Scanner scan = new Scanner(System.in);
while (true) {
System.out.print("Please enter the amount of numbers you want to be calculated. (0 to quit): ");
inputNumber = scan.nextInt();
if (inputNumber == 0) {
System.out.println("Program Ended");
break;
}
for ( int i = 0; i < inputNumber; i++) {
System.out.print("Please enter a value:" );
double addedNumber = scan.nextInt();
numberSum += addedNumber;
}
double numberAverage = numberSum/inputNumber;
System.out.println("Average: " + numberAverage);
}
}
}
But here's the problem:
As you can see, only the first part of the loop gets the average right, but the loop going the second time around doesn't. I'm stuck and how the loop is supposed to go so that the average is calculated right every time instead of just the first. I think it has to do with the initialization, but initializing the variable to 0
, 1
, or -1
didn't help. Plus setting the variable to 0
renders the program to never run since that's the condition in order to stop it. If it does so happen to be a simple initialization error, could someone please just explain what is going on that's wrong with the code instead of saying that this question has been asked before, please?
Upvotes: 0
Views: 117
Reputation: 29265
The code as written is giving the average of every number you've ever entered rather than just the average of the group just entered.
You need to "reset" numberSum
(numberSum = 0;
) at the start of each group of numbers.
Upvotes: 2