I am working on a program that has to check for certain conditions (and give out an error message if they are not executed) before executing two for loops (one for the sum of grades entered and one for highest and lowest grades)and I'm not sure how to sequence them.
Also my code is getting me this error -->Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 and I'm not sure what to do to fix the error
If anyone can help me with the skeletons of the correct sequence I would truly appreciate it!
Here is my code (I know it looks crazy, sorry I'm learning!):
import java.util.Scanner;
public class Homework {
public static void main (String[] args) {
Scanner input = new Scanner(System.in);
int numberOfMarks, average, sum, total, mark;
numberOfMarks = 0;
int [] marks = new int[numberOfMarks];
int smallest, largest;
//System.out.println("Enter number of marks: ");
numberOfMarks = input.nextInt();
if(numberOfMarks<0) {
System.out.println("Number of marks must be greater than 0!!");
}
//System.out.println("Enter "+numberOfMarks+"marks: ");
mark = input.nextInt();
if(mark<0) {
System.out.println("Negative marks not allowed!!!");
} else if(mark>100) {
System.out.println("Marks above 100% not allowed!!!");
}
sum = 0;
for(int i = 0; i < marks.length; i++) {
sum += marks[i];
}
smallest=marks[0];
largest=marks[0];
for(int i=1;i<marks.length;i++) {
if(marks[i]>largest) {
largest=marks[i];
} else if(marks[i]<smallest) {
smallest=marks[i];
}
average = sum/numberOfMarks;
System.out.println("Highest Mark = "+largest);
System.out.println("Lowest Mark = "+smallest);
System.out.println("Average = "+average);
}
}
}