gpt4 book ai didi

java - 初级 Java(使用数组;类分配)

转载 作者:行者123 更新时间:2023-12-02 08:34:16 24 4
gpt4 key购买 nike

我觉得我好像正确地编写了这个作业的代码。我们获得了一个框架和 2 个必须导入的类(FileIOHelper 和 Student)。

 /*
* Created: *** put the date here ***
*
* Author: *** put your name here ***
*
* The program will read information about students and their
* scores from a file, and output the name of each student with
* all his/her scores and the total score, plus the average score
* of the class, and the name and total score of the students with
* the highest and lowest total score.
*/

//
import java.util.Scanner;
import java.io.*;

// C:\Users\Adam\info.txt

public class Lab6
{
public static void main(String[] args) throws IOException
{

// Fill in the body according to the following comments
Scanner key

boardFile = new Scanner(System.in);


// Input file name
String filename = getFileName(keyboardFile);

//Open the file

// Input number of students
int numStudents = FileIOHelper.getNumberOfStudents(filename);
Student students[] = new Student[numStudents];

// Input all student records and create Student array and
// integer array for total scores

int totalScore[] = new int[students.length];
for (int i = 0; i < students.length; i++){
for(int j = 1; j < 4; j++){
totalScore[i] = totalScore[i] + students[i].getScore(j);
}

}



// Compute total scores and find students with lowest and
// highest total score

int maxScore = 0;
int minScore = 0;
for(int i = 0; i < students.length; i++){
if(totalScore[i] >= totalScore[maxScore]){
maxScore = i;
}
else if(totalScore[i] <= totalScore[minScore]){
minScore = i;
}
}

// Compute average total score

int allScores = 0;
int average = 0;
for (int i = 0; i < totalScore.length; i++){
allScores = allScores + totalScore[i];
}

average = allScores / totalScore.length;


// Output results
outputResults(students, totalScore, maxScore, minScore, average);
}

// Given a Scanner in, this method prompts the user to enter
// a file name, inputs it, and returns it.
private static String getFileName(Scanner in)
{
// Fill in the body
System.out.print("Enter the name of a file: ");
String filename = in.next();
return filename;

// Do not declare the Scanner variable in this method.
// You must use the value this method receives in the
// argument (in).
}


// Given the number of students records n to input, this
// method creates an array of Student of the appropriate size,
// reads n student records using the FileIOHelper, and stores
// them in the array, and finally returns the Student array.
private static Student[] getStudents(int n)
{
Student[] myStudents = new Student[n];
for(int i = 0; i <= n; i++){
myStudents[i] = FileIOHelper.getNextStudent();
}
return myStudents;
}

// Given an array of Student records, an array with the total scores,
// the indices in the arrays of the students with the highest and
// lowest total scores, and the average total score for the class,
// this method outputs a table of all the students appropriately
// formatted, plus the total number of students, the average score
// of the class, and the name and total score of the students with
// the highest and lowest total score.
private static void outputResults(
Student[] students, int[] totalScores,
int maxIndex, int minIndex, int average
)
{
// Fill in the body
System.out.println("\nName \t\tScore1 \tScore2 \tScore3 \tTotal");
System.out.println("--------------------------------------------------------");
for(int i = 0; i < students.length; i++){
outputStudent(students[i], totalScores[i], average);
System.out.println();
}
System.out.println("--------------------------------------------------------");
outputNumberOfStudents(students.length);
outputAverage(average);
outputMaxStudent(students[maxIndex], totalScores[maxIndex]);
outputMinStudent(students[minIndex], totalScores[minIndex]);
System.out.println("--------------------------------------------------------");

}

// Given a Student record, the total score for the student,
// and the average total score for all the students, this method
// outputs one line in the result table appropriately formatted.
private static void outputStudent(Student s, int total, int avg)
{
System.out.print(s.getName() + "\t");
for(int i = 1; i < 4; i++){
System.out.print(s.getScore(i) + "\t");
}
System.out.print(total + "\t");
if(total < avg){
System.out.print("-");
}else if(total > avg){
System.out.print("+");
}else{
System.out.print("=");
}
}

// Given the number of students, this method outputs a message
// stating what the total number of students in the class is.
private static void outputNumberOfStudents(int n)
{
System.out.println("The total number of students in this class is: \t" + n);
}

// Given the average total score of all students, this method
// outputs a message stating what the average total score of
// the class is.
private static void outputAverage(int average)
{
System.out.println("The average total score of the class is: \t" + average);
}

// Given the Student with highest total score and the student's
// total score, this method outputs a message stating the name
// of the student and the highest score.
private static void outputMaxStudent(
Student student,
int score
)
{
System.out.println(student.getName() + " got the maximum total score of: \t" + score);
}

// Given the Student with lowest total score and the student's
// total score, this method outputs a message stating the name
// of the student and the lowest score.
private static void outputMinStudent(
Student student,
int score
)
{
System.out.println(student.getName() + " got the minimum total score of: \t" + score);
}
}

但是现在我在 TotalScore[i] = TotalScore[i] + Students[i].getScore(j); 行出现错误

Exception in thread "main" java.lang.NullPointerException

在 Lab6.main(Lab6.java:42)

最佳答案

您的学生数组未初始化。

学生[0]到学生[student.length]均为空

根据您的代码,您应该使用

Student student[] = getStudents(numStudents); 

而不是

Student student[] = new Student[numStudents]; 

关于java - 初级 Java(使用数组;类分配),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2399683/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com