![](/img/trans.png)
[英]How to refractor this code and get the average of all students overall mark in the array
[英]How do I find the highest, lowest and total average of all the students in this Array of students
我本來是一個很好的開始,但在嘗試找到數組的最低和最高等級時陷入困境,還必須找到整個數組的平均等級。
他們告訴這個網站是獲得幫助的最佳場所
所以...如果有人可以幫助我,我將不勝感激
這就是我到目前為止
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();
}
}
}
這是程序的第二部分
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 ];
}
}
您可以使用以下方法簡單地找到平均值,最小值和最大值:
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);
}
在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 。 也許您也想使用其他一些方法。
這樣可以節省您編寫/測試代碼以自行計算值的時間。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.