Got this assignment with the purpose to withdraw a minimum, maximum, and average score from various user inputs. I think that I've got the loop for the data transfer down, thanks to people who commented on the first draft of this post.
public class BattingAverage
{
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
// Declare a named constant for array size here.
int arraySize = 4;
// Declare array here.
double[] averages = new double[arraySize];
// Use this integer variable as your loop index.
int loopIndex;
// Use this variable to store the batting average input by user.
double battingAverage = 0;
// String version of batting average input by user.
String averageString;
// Use these variables to store the minimim and maximum batting averages.
double min, max;
// Use these variables to store the total and the average.
double total = 0;
double average = 0;
// Write a loop to get batting averages from user and assign to array.
System.out.println("Enter a batting average: ");
averageString = s.nextLine();
battingAverage = Double.parseDouble(averageString);
for (double x = 0; x < arraySize; x++) {
averages[x] = s.nextDouble();
}
// Assign the first element in the array to be the minimum and the maximum.
min = averages[0];
max = averages[0];
// Start out your total with the value of the first element in the array.
total = averages[0];
for(Double avgValue:avg) {
total+= averages[x];
}
// Write a loop here to access array values starting with averages[1]
// Within the loop test for minimum and maximum batting averages.
// Also accumulate a total of all batting averages.
// Calculate the average of the 8 averages.
// Print the averages stored in the averages array.
// Print the maximum batting average, minimum batting average, and average batting average.
System.exit(0);
}
}
Fixed the loop to properly take user input and place it in the array, but now im stumped on assignment of the min, max, and total.
On this line:
When you refer to
Scanner, you are referring to the class Scanner and all of the static methods on it.nextDouble()is a member level method of Scanner similar tonextLine(); it can only be called on an instance of class Scanner. If you change it tos.nextDouble()that compilation error should go away.