public class Average { /** * @param args */ publicstaticvoid main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the number of classes: "); intnumClasses = input.nextInt(); int[] numStudents = newint[numClasses]; inttotalStudents = 0; for (inti = 0; i < numClasses; i++) { System.out.print("Enter the number of students in class " + (i + 1) + ":"); numStudents[i] = input.nextInt(); totalStudents += numStudents[i]; } intaverageStudents = totalStudents / numClasses; System.out.println("The average number of students per class is " + averageStudents); System.out.println(); System.out.println("Class\tNumber of Students"); for (inti = 0; i < numClasses; i++) { System.out.print((i + 1) + "\t\t" + numStudents[i]); } } } Write a Java program that solves the following problem. Name your class Average. Your program will determine the average number of students per class in a school. It should begin by requesting the number of classes in the school. It will then create an array and ask for the number of students in each class, storing those values in the array. The program will then print out a report that gives the average, followed by the information about each class. See the sample below for the exact format. Note that we are writing this for users, not programmers, so we are going to start counting at one for the displays to the user. Writing your entire program in main is fine. Please enter the number of classes: 4 Enter the size of class 1: 20 Enter the size of class 2: 15 Enter the size of class 3: 24 Enter the size of class 4: 23 The average class size is 20. Class Number of Students 1 20 2 15 3 24 4 23 Can you help me fix above code so it match the sample output in Java
public class Average
{
/**
* @param args
*/
publicstaticvoid main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of classes: ");
intnumClasses = input.nextInt();
int[] numStudents = newint[numClasses];
inttotalStudents = 0;
for (inti = 0; i < numClasses; i++)
{
System.out.print("Enter the number of students in class " + (i + 1) + ":");
numStudents[i] = input.nextInt();
totalStudents += numStudents[i];
}
intaverageStudents = totalStudents / numClasses;
System.out.println("The average number of students per class is " + averageStudents);
System.out.println();
System.out.println("Class\tNumber of Students");
for (inti = 0; i < numClasses; i++)
{
System.out.print((i + 1) + "\t\t" + numStudents[i]);
}
}
}
Write a Java
Please enter the number of classes: 4
Enter the size of class 1: 20
Enter the size of class 2: 15
Enter the size of class 3: 24
Enter the size of class 4: 23
The average class size is 20.
Class Number of Students
1 20
2 15
3 24
4 23
Can you help me fix above code so it match the sample output in Java

Step by step
Solved in 4 steps with 2 images









