Help with java coding question ... Write a program that allows the user to enter
ID: 3825441 • Letter: H
Question
Help with java coding question ...
Write a program that allows the user to enter students’ names followed by their test scores. Use an array of String to store students’ names and an array of double to store their test scores. The program then outputs the following information (assume that there are 10 students total):
Each student’s name and the test score
Highest test score
The names of all the students having the highest score
Class average score
The names of all the students whose test scores are less than the class average
The names of all the students whose test scores are greater than or equal to the class average
-It outputs the students with scores lower/greater than & equal to the average, but it keeps looping. I also need help with finding the student that has the highest score and outputting that score. Thank you in advance.
1 import java.util.Scanner;
2
3 public class Grades{
4
5 public static void main(String[] args){
6
7 //create a keyboard representing the scanner
8 Scanner console = new Scanner(System.in);
9
10 //define variables
11 double [] score = new double[10];
12
13 String [] name = new String[10];
14 double average = 0.0, sum = 0.0, studentAverage = 0.0, highestScore = 0.0, lowestScore = 0.0;
15
16
17 for(int i= 0; i < score.length; i++){
18
19 System.out.println("Enter the student's name: ");
20 name[i] = console.next();
21 System.out.println("Enter the student's score: ");
22 score[i] = console.nextDouble();
23
24 sum += score[i];
25
26 }//end for loop
27
28 //calculate average
29 average = sum/score.length;
30
31 System.out.println("The average score is: " + average);
32
33
34 int highestIndex = 0;
35
36 for(int i = 1; i < score.length; i++){
37
38 if(score[highestIndex] < score[i]){
39
40 highestIndex = i;
41
42 }
43
44
45 if(score[i] < average){
46 System.out.print(" Names of students whose test scores are less than average: " + name[i]);
47 }
48
49 if(score[i] >= average){
50 System.out.print(" Names of students whose test scores are greater than or equal to average: " + name[i]);
51 }
52
53
54 }//end for loop
55
56 }//end main
57 }//end class
Explanation / Answer
Grades.java
import java.util.Scanner;
public class Grades {
public static void main(String[] args) {
// create a keyboard representing the scanner
Scanner console = new Scanner(System.in);
// define variables
double[] score = new double[10];
String[] name = new String[10];
double average = 0.0, sum = 0.0, studentAverage = 0.0, highestScore = 0.0;
for (int i = 0; i < score.length; i++) {
System.out.print(" Enter the Name of student#"+(i+1)+":");
name[i] = console.next();
System.out.print("Enter the student's score: ");
score[i] = console.nextDouble();
sum += score[i];
}// end for loop
// calculate average
average = sum / score.length;
System.out.println("The average score is: " + average);
int highestIndex = 0;
for (int i = 1; i < score.length; i++) {
if (score[highestIndex] < score[i]) {
highestIndex = i;
}
}// end for loop
System.out.println(" Names of students whose test scores are less than average: ");
for (int i = 1; i < score.length; i++) {
if (score[i] < average) {
System.out.println(name[i]);
}
}
System.out.println(" Names of students whose test scores are greater than or equal to average: ");
for (int i = 1; i < score.length; i++) {
if (score[i] >= average) {
System.out.println(name[i]);
}
}
System.out.println(" "+name[highestIndex]+" got highest score of "+score[highestIndex]);
}// end main
}// end class
_____________________
Output:
Enter the Name of student#1:James
Enter the student's score: 78
Enter the Name of student#2:Tom
Enter the student's score: 88
Enter the Name of student#3:Mike
Enter the student's score: 73
Enter the Name of student#4:Ken
Enter the student's score: 89
Enter the Name of student#5:Richard
Enter the student's score: 93
Enter the Name of student#6:Sachin
Enter the student's score: 65
Enter the Name of student#7:Ricky
Enter the student's score: 85
Enter the Name of student#8:Ben
Enter the student's score: 63
Enter the Name of student#9:Cook
Enter the student's score: 95
Enter the Name of student#10:Kenny
Enter the student's score: 56
The average score is: 78.5
Names of students whose test scores are less than average:
Mike
Sachin
Ben
Kenny
Names of students whose test scores are greater than or equal to average:
Tom
Ken
Richard
Ricky
Cook
Cook got highest score of 95.0
_________________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.