the question im doing is:
write a java program that allows the user to enter a set of numbers and then displays the sum and the average of the values. It should begin by asking the user how many values they will enter. you should use two seperate methods to compute the sum and the average.(note that the average is the sum divided by the number of values, so the average method should call the sum method)
This is what I have so far:
import javax.swing.JOptionPane;
public class maths_comp
{
public static void main(String[]args)
{
//Declare variables
String input;
double iterations;
double Sum;
input = JOptionPane.showInputDialog("Enter the amount of numbers to be entered:");
iterations = Double.parseDouble(input);
JOptionPane.showMessageDialog(null, "The Sum of the " + iterations + " numbers entered is: " + sum(iterations) + "\nThe Average of the numbers entered is: " + average(iterations));
}
public static double sum(double iter)
{
String SumInput;
double NewNum;
int count = 1;
double Total = 0;
while(count <= iter)
{
SumInput = JOptionPane.showInputDialog("Enter number " + count + ":");
NewNum = Double.parseDouble(SumInput);
Total = Total + NewNum;
count = count + 1;
}
return Total;
}
public static double average(double iter)
{
double AvgNum;
AvgNum = sum(iter) / iter;
return AvgNum;
}
}
The problem im having is that my program asks for the values again when calculating the average...how do I get it to ask the user only once?