The assignment is to use a a While loop to take the first and last numbers and to find the average of the numbers in between. I have the code typed out and I think it works mathematically but I keep on getting an error when I'm trying to display the answer ("Answer is" + average), it keeps on saying that the Average hasn't been Initialized.
public static void whileLoop()
{
int smallNumber = Integer.parseInt(JOptionPane.showInputDialog("Enter the smallest number:"));
int largeNumber = Integer.parseInt(JOptionPane.showInputDialog("Enter the largest number:"));
int counter = smallNumber;
int average;
int total = 0;
int numberCounter = 0;
while (smallNumber <= largeNumber)
{
total = counter + total;
counter = counter + 1;
numberCounter++;
average = total / numberCounter;
}
JOptionPane.showMessageDialog(null, "Answer is: " + average);
}