简体   繁体   中英

How do I find the highest, lowest and total average of all the students in this Array of students

I was of to a good start but got stuck trying to find the lowest and highest grade of the array.I also have to find the average grade of the whole array.

They told this website was the best place to get help

SO ... if anyone can help me I'll appreciate it

This is what i have so far

import java.util.Scanner;

public class Grades
{
 public static void main(String[]args)
{
  Scanner keyboard = new Scanner(System.in);
  String studentName;
  double studentScore;
  double [][]studentTestScores;
  double Average ;
  char LetterGrade;
  double LowestScore;


  final int numStudents = 2;
  final int numGrades = 3;


  Grades2 class1 = new Grades2 (numStudents,numGrades );
  studentTestScores = class1.getTestScoresArray();

  for( int StudentIndex = 0; StudentIndex < numStudents; StudentIndex++)
  {
     System.out.println("Enter name of student " + (StudentIndex + 1));
     studentName = keyboard.nextLine();
     class1.setStudentName( studentName );



     for( int studentScoreIndex = 0; studentScoreIndex < numGrades; studentScoreIndex++)
     {
        System.out.println("Enter test grade " + (studentScoreIndex + 1) + " grade of " + studentName);
        studentScore = keyboard.nextDouble();

        while( studentScore < 0 || studentScore > 100){
           System.out.println("Please enter a valid 
score" + (studentScoreIndex + 1) + " for " + 
studentName);
           studentScore = keyboard.nextDouble();
     }

        keyboard.nextLine();
        class1.setScore(StudentIndex, studentScoreIndex, studentScore );            
     }
  } 


  for( int StudentIndex = 0; StudentIndex < n 
umStudents; StudentIndex++ ){
     System.out.print( class1.getStudentName( 
StudentIndex ) + " Test scores are ");

     Average    = class1.AverageTestScore( 
studentTestScores[ StudentIndex ] ); 
     LetterGrade = class1.getLetterGrade( Average );
     LowestScore = class1.getLowestScore( 
studentTestScores[ StudentIndex ]  );


     for( int studentScoreIndex =  
0;studentScoreIndex < numGrades; 
studentScoreIndex++){
        if( studentScoreIndex != numGrades  -1){
           System.out.print( studentTestScores[ 
StudentIndex ][ studentScoreIndex ] + ", " );
        } else { 
           System.out.print( studentTestScores[ 
StudentIndex ][ studentScoreIndex ] + " " );
        }
     }

     System.out.println("with an average of " + 
 String.format(" %.2f", Average) + ", letter grade " 
+ LetterGrade );
     System.out.println("\nLowest grade is " + 
LowestScore);
     System.out.println();

  }
}

}

Here is part 2 of the program

import java.util.ArrayList;

public class Grades2
{

private ArrayList <String> studentNames = new 
ArrayList <String> ();
private char [] LetterGrades = {'A', 'B', 'C' , 'D' 
, 
'F' };
  private double [] [] studentTestScores;


public String getStudentName(int studentIndex){
  return studentNames.get(studentIndex);
}



public double AverageTestScore( double [] 
studentTestScores){
  double studentTestScoresTotal = 0;
  double studentTestScoresAverage;

  for( int studentTestScore = 0; studentTestScore < 
studentTestScores.length; studentTestScore++){
     studentTestScoresTotal = studentTestScoresTotal 
+ studentTestScores[ studentTestScore];
  }
  studentTestScoresAverage = studentTestScoresTotal 
/ studentTestScores.length;
  return studentTestScoresAverage;
}


public double getLowestScore(){
  double LowestScore = studentTestScores[0] ;

  for( int studentTestScore = 0; studentTestScore < 
studentTestScores.length; studentTestScore++){
     if ( studentTestScores[scoreIndex] < 
LowestScore){
     LowestScore = studentTestScores[scoreIndex];

}
}
  return LowestScore;

}


public char getLetterGrade( double TestScoreAverage 
    )
{
  char  LetterGrade = 'Z';

  if(TestScoreAverage < 60 ){
     LetterGrade = 'F';
  } else if ( TestScoreAverage < 70){
     LetterGrade = 'D';
  } else if ( TestScoreAverage < 80){
     LetterGrade = 'C';
  } else if ( TestScoreAverage < 90){
     LetterGrade = 'B';
  } else if ( TestScoreAverage < 100){
     LetterGrade = 'A';
  }

  return LetterGrade;

}

public void setStudentName(String studentName){
  studentNames.add(studentName);

}

public void setScore(int studentIndex, int 
scoreIndex, double Score){
  studentTestScores[ studentIndex ][ scoreIndex ] = 
Score;

}

public double [][] getTestScoresArray(){
  return studentTestScores;
}      

public Grades2( int numStudents, int numGrades){
  studentTestScores = new double [ numStudents ][ 
numGrades ];
}
}

You can simply find average, min and max by using following :

public double AverageTestScore(double[] studentTestScores) {
    return Arrays.stream(studentTestScores).average().orElse(0);
}

public double getLowestScore(double[] scores) {
    return Arrays.stream(scores).min().orElse(0);
}

public double getHighestScore(double[] scores) {
    return Arrays.stream(scores).max().orElse(0);
}

Within the second for-loop of the main -method you can use the following code to get for each student ( studentTestScores[StudentIndex] ). It gives you the " highest, lowest and total average " values you asked for:

DoubleSummaryStatistics statistics = Arrays.stream(studentTestScores[StudentIndex])
        .boxed()
        .collect(Collectors.summarizingDouble(Double::doubleValue));
double lowestScore = statistics.getMin();
double averageScore = statistics.getAverage();

Have a look on the JavaDoc of DoubleSummaryStatistics . Maybe you want to use some of the other methods too.

This should save you to write/test the code to compute the values on your own.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM