Hello, I am pretty new to programming and needed some help with one of my programs. I am trying to have the user input how many numbers they want to plug in then they plug in x amount of numbers until they reach whatever they input. After that I am trying to create three methods which do different functions. One method calculates the sum of all the input numbers. Second method calculates the average of all input numbers. Fourth method displays all of the results. I am not looking for someone to do all o the work for me, but I am seeking guidance to figure out how I would use information from one method in another without using arrays (we haven't gotten that far in the class yet, and I want to learn this).
This is the code I have so far:
// Import JOptionPane
import javax.swing.JOptionPane;
public class hidingmyname {
public static void main(String[] args) {
// Declare variables
int LoopEnd = 1;
double InputNumbers;
// Asks user how many numbers they would like to input
String HowManyNumbersstring = JOptionPane.showInputDialog(null, "How many numbers would you like to input?", "?", JOptionPane.QUESTION_MESSAGE);
// Convert string into integer
int HowManyNumbers = Integer.parseInt(HowManyNumbersstring);
// Output message notifying how many numbers the user is plugging in
System.out.println("Your " + HowManyNumbers + " numbers are: ");
do {
// Prompts user to input their integers
String InputNumbersstring = JOptionPane.showInputDialog(null, "Enter an integer: ", "Input", JOptionPane.QUESTION_MESSAGE);
// Convert string into double
InputNumbers = Double.parseDouble(InputNumbersstring);
// If statement for outputting numbers for user to see in display box
if (HowManyNumbers == LoopEnd){
System.out.println(InputNumbers + ".");
LoopEnd++;
}
else if (LoopEnd % 10 == 0){
System.out.println(InputNumbers + ". ");
LoopEnd++;
}
else {
System.out.print(InputNumbers + ", ");
LoopEnd++;
}
} while (LoopEnd <= HowManyNumbers);
System.out.println("The sum of all your numbers is " + calculateSum(HowManyNumbers, InputNumbers, LoopEnd));
}
// Create new method to add all numbers that were input by user
public static double calculateSum(int HowManyNumbers, double InputNumbers, int LoopEnd){
double calculateSum = 0.0;
int sum = 0;
while (LoopEnd <= HowManyNumbers){
calculateSum = sum + InputNumbers;
}
return calculateSum;
}
}
At the moment I am having issues trying to get the input values from the first method over to the second method to calculate the sum. It seems like the while loop in the second method is not adding like I want it to.
Any and all help is appreciated.