我如何找到这个学生数组中所有学生的最高、最低和总平均数
Posted
技术标签:
【中文标题】我如何找到这个学生数组中所有学生的最高、最低和总平均数【英文标题】:How do I find the highest, lowest and total average of all the students in this Array of students 【发布时间】:2019-03-01 20:19:24 【问题描述】:我有一个良好的开端,但在试图找到阵列的最低和最高等级时遇到了困难。我还必须找到整个阵列的平均等级。
他们告诉这个网站是获得帮助的最佳场所
所以...如果有人可以帮助我,我将不胜感激
这就是我目前所拥有的
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();
这是程序的第 2 部分
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 ];
【问题讨论】:
那么您遇到的具体问题是什么? Java 中的变量和方法应该是小写的。至少,我建议调整您的方法 AverageTestScore 以与其他小写方法保持一致。 您使用的是哪个 Java 版本? Chota Bheem 的答案适用于 Java 8 或更高版本。下面你想看看this answer,特别是第二个代码块。 【参考方案1】:您可以使用以下方法简单地找到平均值、最小值和最大值:
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);
【讨论】:
【参考方案2】:在 main
方法的第二个 for 循环中,您可以使用以下代码获取每个学生 (studentTestScores[StudentIndex]
)。它为您提供了您要求的“最高、最低和总平均”值:
DoubleSummaryStatistics statistics = Arrays.stream(studentTestScores[StudentIndex])
.boxed()
.collect(Collectors.summarizingDouble(Double::doubleValue));
double lowestScore = statistics.getMin();
double averageScore = statistics.getAverage();
查看DoubleSummaryStatistics
的JavaDoc。也许您也想使用其他一些方法。
这应该让您不必编写/测试代码来自己计算值。
【讨论】:
以上是关于我如何找到这个学生数组中所有学生的最高、最低和总平均数的主要内容,如果未能解决你的问题,请参考以下文章
sql,查询每门课程最高分的学生的学号,课程号,成绩。再一张表里