我正在研究项目的最后一部分,在尝试更新学生的成绩时遇到困难。请参阅下面的updateGrade部分:
import java.util.ArrayList;
public class Roster {
private ArrayList<Student> students;
private int[] gradingBreakdown;
/**
* Constructor to create a Roster object.
*
*/
public Roster() {
this.students = new ArrayList<Student>();
}
/**
* Adds the specified Student to the Roster
* @param newStudent The Student to be added
* @precondition newStudent != null
*/
public void addStudent(Student newStudent) {
if (newStudent == null) {
return;
} else {
this.students.add(newStudent);
}
}
/**
* Accepts Student object to be removed
* @param oldStudent The Student to be removed
* @precondition oldStudent != null
*/
public void removeStudent(Student oldStudent) {
if (oldStudent == null) {
return;
} else {
this.students.remove(oldStudent);
}
}
/**
* Finds and returns the first student that matches the specified last name.
*
* @param lastName Student to search for
* @precondition lastName != null
* @return Found Student object or null if not found.
*/
public Student findStudent(String lastName) {
if (lastName == null) {
return null;
}
for (Student currStudent : this.students) {
if (currStudent.getLastName().equalsIgnoreCase(lastName)) {
return currStudent;
}
}
return null;
}
**/**
* Sets the grade of the specified student to the new grade.
*
* @param lastName Student to find.
* @param grade Grade to update to.
* @precondition lastName != null
* 0 <= grade <= 100
*/
public void updateGrade(String lastName, int grade) {
if (lastName == null) {
return;
} else {
this.findStudent(lastName);
}
}**
/**
* Builds and return an output string of the class roster
*
* @return A string representation of the class roster.
*/
public String toString() {
if (this.students.isEmpty()) {
return "There are no students enrolled";
}
String result = "Class roster\n";
for (Student currStudent : this.students) {
result += currStudent.getFirstName() + " "
+ currStudent.getLastName() + ": " + currStudent.getGrade()
+ "\n";
}
result += "\nMin grade: " + this.getMinimumGrade() + "\n";
result += "Max grade: " + this.getMaximumGrade() + "\n";
result += String.format("Ave grade: %.2f\n", this.getAverageGrade());
return result;
}
/**
* Builds and returns a summary of the grading breakdown which returns
* number of A's, B's, C's, etc.
*
* @return The grading breakdown.
*/
public String getGradingBreakdown() {
this.determineGradingBreakdown();
String gradeOutput;
gradeOutput = "A: " + this.gradingBreakdown[0] + "\n";
gradeOutput += "B: " + this.gradingBreakdown[1] + "\n";
gradeOutput += "C: " + this.gradingBreakdown[2] + "\n";
gradeOutput += "D: " + this.gradingBreakdown[3] + "\n";
gradeOutput += "F: " + this.gradingBreakdown[4] + "\n";
return gradeOutput;
}
/**
* Builds and returns a grading histrogram which returns number of A's, B's,
* C's, etc.
*
* @return The grading histrogram
*/
public String getGradingHistogram() {
this.determineGradingBreakdown();
String histogram;
histogram = "A: " + this.starLine(this.gradingBreakdown[0]) + "\n";
histogram += "B: " + this.starLine(this.gradingBreakdown[1]) + "\n";
histogram += "C: " + this.starLine(this.gradingBreakdown[2]) + "\n";
histogram += "D: " + this.starLine(this.gradingBreakdown[3]) + "\n";
histogram += "F: " + this.starLine(this.gradingBreakdown[4]) + "\n";
return histogram;
}
private int getMinimumGrade() {
int min = Integer.MAX_VALUE;
for (Student currStudent : this.students) {
if (currStudent.getGrade() < min) {
min = currStudent.getGrade();
}
}
return min;
}
private int getMaximumGrade() {
int max = Integer.MIN_VALUE;
for (Student currStudent : this.students) {
if (currStudent.getGrade() > max) {
max = currStudent.getGrade();
}
}
return max;
}
private double getAverageGrade() {
int sum = 0;
for (Student currStudent : this.students) {
sum += currStudent.getGrade();
}
return sum / (double) this.students.size();
}
private void determineGradingBreakdown() {
this.gradingBreakdown = new int[5];
for (Student currStudent : this.students) {
int grade = currStudent.getGrade();
if (grade >= 90) {
this.gradingBreakdown[0]++;
} else if (grade >= 80) {
this.gradingBreakdown[1]++;
} else if (grade >= 70) {
this.gradingBreakdown[2]++;
} else if (grade >= 60) {
this.gradingBreakdown[3]++;
} else {
this.gradingBreakdown[4]++;
}
}
}
private String starLine(int numberStars) {
String result = "";
for (int counter = 0; counter < numberStars; counter++) {
result += "*";
}
return result;
}
}
这是我的第二堂课:
import java.util.Scanner;
/**
* Menu-based textual user interface for Roster
*
*/
public class RosterTUI {
private Scanner scan;
private Roster userRoster;
/**
* Instantiates scanner and assigns Roster object to
* instance variable
*
* @param object new Roster object
*/
public RosterTUI(Roster object) {
this.userRoster = new Roster();
this.scan = new Scanner(System.in);
}
/**
* Run, print welcome message
*/
public void run() {
int choice = 0;
System.out.println("Please enter your name: ");
String userName = this.scan.nextLine();
System.out.println("\nWelcome " + userName + "!\n");
while (choice != 6) {
this.displayMenu();
System.out.println("Please select an option:");
String input = this.scan.nextLine();
choice = Integer.parseInt(input);
if (choice == 1) {
this.addStudent();
} else if (choice == 2) {
this.removeStudent();
} else if (choice == 3) {
this.updateGrade();
}else if (choice == 4) {
this.displayStatistics();
} else if (choice == 5) {
this.displayBreakdown();
} else {
if (choice != 6) {
System.out.println("Not a valid option.\n");
}
}
}
System.out.println("\nThank you for using the application.");
}
/**
* Menu options for user
*/
public void displayMenu() {
System.out.println("Main Menu\n");
System.out.println("1 - Add a student");
System.out.println("2 - Remove a student");
System.out.println("3 - Update a student");
System.out.println("4 - Display class statistics");
System.out.println("5 - Display grading breakdown");
System.out.println("6 - Quit\n");
}
/**
* Adds student based off user input
*/
public void addStudent() {
System.out.println("Please enter the first name of the student: ");
String firstName = this.scan.nextLine();
System.out.println("Please enter the last name of the student: ");
String lastName = this.scan.nextLine();
System.out.println("Please enter the grade of the student between 0 and 100: ");
String grade = this.scan.nextLine();
int userGrade = Integer.parseInt(grade);
if (userGrade > 100 || userGrade < 0) {
System.out.println("Grade must be between 0 and 100. Please enter grade: ");
grade = this.scan.nextLine();
userGrade = Integer.parseInt(grade);
}
Student userStudent = new Student(firstName, lastName, userGrade);
this.userRoster.addStudent(userStudent);
}
/**
* This method displays the statistics
*/
public void displayStatistics() {
System.out.println("\n" + this.userRoster.toString());
}
/**
* Display grading breakdown and histogram
*/
public void displayBreakdown() {
System.out.println("\n" + this.userRoster.getGradingBreakdown());
System.out.println(this.userRoster.getGradingHistogram());
}
/**
* Removes Student based off last name entered by user
*/
public void removeStudent() {
System.out.println("Please enter the last name of the student to be removed: ");
String lastName = this.scan.nextLine();
if (this.userRoster.findStudent(lastName) == null) {
System.out.println("A student with the last name of " + lastName + " does not exist.\n");
} else {
this.userRoster.removeStudent(this.userRoster.findStudent(lastName));
System.out.println(lastName + " has been deleted.\n");
}
}
/**
* Updated the grade of a student after finding by last name
*/
public void updateGrade() {
System.out.println("Please first enter the last name of the student: ");
String lastName = this.scan.nextLine();
if (this.userRoster.findStudent(lastName) == null) {
System.out.println("A student with the last name of " + lastName + " does not exist.\n");
} else if (this.userRoster.findStudent(lastName) != null) {
System.out.println("Please enter the new grade of the student between 0 and 100: ");
String grade = this.scan.nextLine();
int userGrade = Integer.parseInt(grade);
if (userGrade > 100 || userGrade < 0) {
System.out.println("Grade must be between 0 and 100. Please enter new grade: ");
grade = this.scan.nextLine();
userGrade = Integer.parseInt(grade);
}
this.userRoster.updateGrade(lastName, userGrade);
}
}
}
我为每个updateGrade部分包含了我到目前为止的代码(抱歉,要滚动查看主要内容的代码很多)。我不确定我的问题出在哪一端,但程序的其余部分完全按照应有的方式工作。任何有关updateGrade方法的帮助都将不胜感激。
谢谢!
答案 0 :(得分:0)
在Roster#updateGrade(String, int)
中,您需要找到Student
:
this.findStudent(lastName);
但不要使用此方法调用的结果。你可能想要:
Student foundStudent = this.findStudent(lastName);
if (foundStudent != null) {
// found
foundStudent.setGrade(grade);
} else {
// not found..
}
或类似的东西。
作为旁注,您应该直接在Roster
类