忍受我,这有点长。这节课在这里创建一个学生对象:
public class Student {
String firstName;
String lastName;
int assignmentScores[];
int labScores[];
int attendanceScore;
int totalHomeworkScore;
int midterm1;
int midterm2;
int finalExam;
int zyanteScore;
int patScore;
int totalTestScore;
String letterGrade;
public Student() {
}
public void setFirstName(String fName) {
firstName = fName;
}
public String getFirstName() {
return firstName;
}
public void setLastName(String lName) {
lastName = lName;
}
public String getLastName() {
return lastName;
}
public void setAssignmentScores(int[] assignmentScore) {
assignmentScores = assignmentScore;
}
public int[] getAssignmentScores() {
return assignmentScores;
}
public void setLabScores(int[] labScore) {
assignmentScores = labScore;
}
public int[] getLabScores() {
return labScores;
}
public void setAttendanceScore(int attenScore) {
attendanceScore = attenScore;
}
public int getAttendanceScore() {
return attendanceScore;
}
public void setTotalHomeworkScore(int hScore) {
totalHomeworkScore = hScore;
}
public int getTotalHomeworkScore() {
return totalHomeworkScore;
}
public void setMidTerm1(int mT1) {
midterm1 = mT1;
}
public int getMidterm1() {
return midterm1;
}
public void setMidterm2(int mT2) {
midterm2 = mT2;
}
public int getMidterm2() {
return midterm2;
}
public void setFinalExam(int fExam) {
finalExam = fExam;
}
public int getFinalExam() {
return finalExam;
}
public void setZyanteScore(int zyant) {
zyanteScore = zyant;
}
public int getZyanteScore() {
return zyanteScore;
}
public void setPatScore(int pat) {
patScore = pat;
}
public int getPatScore() {
return patScore;
}
public void setTotalTestScore(int tScore) {
totalTestScore = tScore;
}
public int getTotalTestScore() {
return totalTestScore;
}
public void computeGrade() {
if (getTotalHomeworkScore() <= 599 || getTotalTestScore() <= 149
|| getTotalHomeworkScore() <= 719 && getTotalTestScore() <= 179
|| getTotalHomeworkScore() <= 779 && getTotalTestScore() <= 164
|| getTotalHomeworkScore() <= 659 && getTotalTestScore() <= 209) {
letterGrade = "P";
}
if (getTotalHomeworkScore() >= 1140 && getTotalTestScore() >= 180
|| getTotalHomeworkScore() >= 1080
&& getTotalTestScore() >= 195 || getTotalHomeworkScore() >= 960
&& getTotalTestScore() >= 210 || getTotalHomeworkScore() >= 900
&& getTotalTestScore() >= 225 || getTotalHomeworkScore() >= 840
&& getTotalTestScore() >= 240 || getTotalHomeworkScore() >= 780
&& getTotalTestScore() >= 255 || getTotalHomeworkScore() >= 720
&& getTotalTestScore() >= 285) {
letterGrade = "G";
} else {
letterGrade = "A";
}
}
public String getGrade() {
return letterGrade;
}
}
此类创建一个学生对象,其值可以从文本文件中设置。下一个类创建了这些学生对象的数组,以及其他一些目前不重要的东西。现在重要的方法是setStudents方法,它创建学生对象数组:
public class CourseOffering {
Student[] students;
String description;
double homeworkAverage;
double testAverage;
int passingStudents;
public CourseOffering() {
}
public void setStudents(Student[] studentArray) {
students = studentArray;
}
public void setDescription(String descript) {
description = descript;
}
public String getDescription() {
return description;
}
public double computeHomeworkAverage() {
int temp = 0;
for (int i = 0; i < students.length; i++) {
temp += students[i].getTotalHomeworkScore();
}
homeworkAverage = temp / students.length;
return homeworkAverage;
}
public double computeTestAverage() {
int temp = 0;
for (int j = 0; j < students.length; j++) {
temp += students[j].getTotalTestScore();
}
testAverage = temp / students.length;
return testAverage;
}
public int countPassingStudents() {
int temp = 0;
for (int k = 0; k < students.length; k++) {
if (students[k].getGrade() == "G") {
temp++;
}
}
passingStudents = temp;
return passingStudents;
}
}
最后,这个类是运行整个事物的驱动程序:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class CourseStatistics {
static int numberOfClasses = 3;
static int numberOfStudents = 4;
static int numberOfAssignments = 7;
public static void main(String[] args) {
CourseOffering myCourseOffering = new CourseOffering();
Student myStudent = new Student();
myCourseOffering.students = new Student[numberOfStudents];
Scanner scanner = new Scanner(System.in);
try {
scanner = new Scanner(new File("gradesA5.txt"));
} catch (FileNotFoundException e) {
System.out
.println("Error opening file. Please make sure that you have a grades.txt file in the same folder as GradeCalculator.class");
System.exit(0);
}
numberOfClasses = scanner.nextInt();
System.out.println(numberOfClasses);
for (int i = 0; i < numberOfClasses; i++) {
for (int j = 0; j < numberOfStudents; j++) {
myCourseOffering.students[i] = new Student();
myCourseOffering.setDescription(scanner.next()); // CSCE
myCourseOffering.setDescription(scanner.next()); // 155A
myCourseOffering.setDescription(scanner.next()); // -
myCourseOffering.setDescription(scanner.next()); // Reads
// Semester
System.out.print(myCourseOffering.getDescription() + " "); // Prints
// Semester
myCourseOffering.setDescription(scanner.next()); // Reads Year
System.out.println(myCourseOffering.getDescription()); // Prints
// Year
numberOfStudents = scanner.nextInt(); // Number Of Students
System.out.println(numberOfStudents); // Prints number of
// students
System.out.println("Name" + "\t" + "\t" + "Assignment Score"
+ "\t" + "Test Score" + "\t" + "Grade");
myCourseOffering.students[j].setFirstName(scanner.next());
System.out.print(myCourseOffering.students[j].getFirstName()
+ " ");
myCourseOffering.students[j].setLastName(scanner.next());
System.out.print(myCourseOffering.students[j].getLastName());
for (int k = 0; k < numberOfAssignments; k++) {
myCourseOffering.students[j].assignmentScores[k].setAssignmentScores(scanner.nextInt());
}
}
}
}
}
我无法弄清楚如何在courseOfferign中调用学生对象数组中的数组。我想称这两种方法:
public void setAssignmentScores(int[] assignmentScore) {
assignmentScores = assignmentScore;
}
public int[] getAssignmentScores() {
return assignmentScores;
}
它们都是CourseOffering中学生对象数组的一部分。我尝试做类似的事情,当我从学生对象数组中得到名字和姓氏时,我在这个for循环中做了:
for (int k = 0; k < numberOfAssignments; k++) {
myCourseOffering.students[j].assignmentScores[k].setAssignmentScores(scanner.nextInt());
}
但显然这不起作用。我正在尝试使用文本文件中的数字行填充assignmentScore数组。我假设我必须以某种方式初始化一个新数组,但我不知道如何以及在何处执行它。 这是我试图阅读的文本文件:
3
CSCE 155A - Fall 2011
4
Anthony Hopkins 80 90 95 87 80 78 25 17 20 22 21 24 19 22 21 23 24 21 20 25 20 55 56 110 30 20 25 8
John Smith 99 95 82 72 64 52 15 14 11 21 25 12 19 20 21 23 21 12 12 10 15 50 50 60 25 15 20 9
Pan Mei 85 92 72 45 82 78 22 13 16 22 24 10 18 12 21 24 25 10 11 14 20 58 51 95 28 14 28 7
Rafael Vega 99 45 87 52 87 99 25 25 21 21 14 19 19 25 25 20 20 18 20 24 20 60 60 60 25 16 23 8
CSCE 155A - Spring 2012
1
Paul Kubi 80 90 5 87 80 0 25 0 20 22 21 24 19 22 21 0 24 21 20 25 20 0 0 0 30 20 25 8
CSCE 155A - Fall 2012
3
Tianna Delp 99 99 99 99 99 99 24 15 16 21 25 15 19 20 21 22 21 21 23 15 15 60 50 60 20 17 20 9
Taylor Delp 95 92 80 90 82 78 25 25 25 25 24 10 25 25 25 25 25 25 25 25 25 58 51 95 28 14 28 7
Rachel Valenz 99 45 87 52 87 99 25 25 21 21 14 19 19 25 25 20 20 18 20 24 20 60 60 60 25 16 23 8
答案 0 :(得分:2)
你的方法setAssignmentScores
期望一个int数组作为参数,你发送一个整数,这是不正确的。
myCourseOffering.students[j].assignmentScores[k].setAssignmentScores(scanner.nextInt());
相反,构建一个数组并传递它
int[] myArray = new int[numberOfAssignments]; // notice this
for (int k = 0; k < numberOfAssignments; k++) {
myArray[i] = scanner.nextInt();
}
myCourseOffering.students[j].assignmentScores[k].setAssignmentScores(myArray);
答案 1 :(得分:1)
public void setAssignmentScores(int[] assignmentScore)
您通过传递整数而不是数组来调用此函数:
myCourseOffering.students[j].assignmentScores[k].setAssignmentScores(scanner.nextInt());
尝试立即读取分配分数以保存在数组中,然后使用数组调用此函数。
int assignScores[] = new int[numberOfAssignments] ;
for (int k = 0; k < numberOfAssignments; k++) {
assignScores[k] = scanner.nextInt();
}
myCourseOffering.students[j].assignmentScores[k].setAssignmentScores(assignScores);
答案 2 :(得分:1)
如果您不想直接暴露数组,那么您可以选择两种相同的方式:
1)构建数组然后设置它
int[] tempAssignments = new int[numberOfAssignments];
for (int i = 0; i < numberOfAssignments; ++i)
tempAssignments[i] = scanner.nextInt();
student.setAssignmentScores(tempAssignments);
2)在Student
中初始化数组并用数据填充
class Student {
void initializeAssignments(int count) {
assignmentScores = new int[count];
}
void setAssignmentScore(int index, int value) {
assignmentScores[index] = value;
..
}
student.initializeScores(numberOfAssignments);
for (int i = 0; i < numberOfAssignments; ++i)
student.setAssignmentScore(i, scanner.nextInt());
}
或者您可以使用List<Integer>
并安居乐业:
class Student {
final List<Integer> assignmentScores = new ArrayList<Integer>();
void addScore(int score) {
assignmentScore.add(score);
}
}
for (int i = 0; i < numberOfAssignments; ++i)
student.addScore(scanner.nextInt());
答案 3 :(得分:1)
问题出在
行myCourseOffering.students[j].assignmentScores[k].setAssignmentScores(scanner.nextInt());
assignmentScores
是一个int
的数组,setAssignmentScores()
是类Student
的方法,而不是类int
。在.assignmentScores[k].setAssignmentScores(...
中,您试图调用int
的方法.setAssignmentScores(...
,该方法不存在。相反,您需要调用Student
的方法。
请查看下面的代码,该代码应替换行myCourseOffering.students[j].assignmentScores[k].setAssignmentScores(scanner.nextInt());
int[] scores = new int[numberOfAssignments];
for(int k = 0; scanner.hasNextInt(); k++)
{
scores[k] = scanner.nextInt();
}
myCourseOffering.students[j].setAssignmentScores(scores);
答案 4 :(得分:0)
使assignmentScores成为ArrayList或Collection。
我会创建一个名为addAssignmentScore()
的方法,它会添加到分配分数数组中。