我本来是一个很好的开始,但是在尝试找到数组的最低和最高等级时陷入困境。我还必须找到整个数组的平均等级。
他们告诉该网站是获得帮助的最佳场所
所以...如果有人可以帮助我,我会感激
这是我到目前为止所拥有的
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 ];
}
}
答案 0 :(得分: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);
}
答案 1 :(得分: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。也许您也想使用其他一些方法。
这样可以节省您编写/测试代码以自行计算值的时间。