I have to find the average, highest, and lowest numbers in an array. I already have the average and highest, and I thought I could find the lowest number the same way I found the highest, but it's not working. It just keeps coming out to 0. Please help, I'm pretty confused and I've tried out different ways, but I want to see if there are better ways than doing MAX_VALUE for the lowest, then looping it through.
These are the resultsimport java.util.Scanner; public class Test_Scores { public static void main(String[] args) { //array,scanner,values Scanner input = new Scanner(System.in); double[] allScores = new double[5]; double sum = 0; double average = 0; double highest = allScores[0]; double lowest = allScores[0]; for(int i = 0; i < allScores.length; i++) { System.out.print("Enter score for test " + (i + 1) + ": "); allScores[i] = input.nextDouble(); } //Average grade for(int a = 0; a < allScores.length; a++) { sum += allScores[a]; average = sum / 5; } //Highest grade for(int h = 0; h < allScores.length; h++) { if (allScores[h] > highest) { highest = allScores[h]; } } //Lowest grade for(int l = 5; l < allScores.length; l--) { if (allScores[l] < lowest) { lowest = allScores[l]; } } //output System.out.println("The average grade is: " + average); System.out.println("The highest grade is: " + highest); System.out.println("The lowest grade is: " + lowest); } }
Enter score for test 1: 98 Enter score for test 2: 96 Enter score for test 3: 92 Enter score for test 4: 65 Enter score for test 5: 85 The average grade is: 87.2 The highest grade is: 98.0 The lowest grade is: 0.0