Hey, I have to write a program that calculates the average of a set of numbers input by the user. I am getting “exit status 1” and can’t figure out what I’m doing wrong to get this error.
Any input, or direction as to where to look to fix this would be great.
import java.util.Scanner;
public class AverageCalculator {
public static void main(String args) {
Scanner input = new Scanner(System.in);
int numInputs = 5; // variable to store the number of inputs
double totalSum = 0; // variable to store the sum of the inputs
// Hardcode the inputs
double[] inputs = {2, 4, 6, 8, 10};
// Loop through the inputs and add to the total sum
for (double num : inputs) {
totalSum += num;
}
// Calculate the average if at least one number was inputted
if (numInputs > 0) {
double average = totalSum / numInputs;
System.out.println("The average of the inputs is: " + average);
}
else {
System.out.println("No inputs were entered. Cannot calculate average.");
}
}
}
Your code is correct, but the compiler is specific on how you format it,
public class AverageCalculator { is not valid, as it should have been declared in the file AverageCalculator.java (Should be changed to public class Main { for your java file reference, Main.java)
import java.util.Scanner;
public class Main { // Changed AverageCalculator to Main
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int numInputs = 5; // variable to store the number of inputs
double totalSum = 0; // variable to store the sum of the inputs
// Hardcode the inputs
double[] inputs = {2, 4, 6, 8, 10};
// Loop through the inputs and add to the total sum
for (double num : inputs) {
totalSum += num;
}
// Calculate the average if at least one number was inputted
if (numInputs > 0) {
double average = totalSum / numInputs;
System.out.println("The average of the inputs is: " + average);
}
else {
System.out.println("No inputs were entered. Cannot calculate average.");
}
}
}
Hey, you originally had ‘JS’ as your title. I just wanted to let you know JS stands for JavaScript which is a completely different programming language from Java.