0

I have an assignment that asks me to create students with first name, last name, GPA and major as options the user can input and I am supposed to give these "students" a student ID as well. I can give them an ID in the constructor of one of the classes but I can't seem to iterate through the student count, to give the students the correct type of student id, eg. 123456, 123457, 123458 etc. I am only pasting a few lines because the whole assignment is about 300 lines long and I didn't think anyone cared to read it all over. Can anyone tell me what I'm doing wrong or if this doesn't even make sense to try? I know of another way but I don't like it because it makes me have the student id numbers stored in a separate ArrayList than the other student data and then I would just match up indices. Here's the constructor, with count being initialized as a field with 0...

public Student( String fName, String lName, String maj, double gpa) {
    sNumber += count++;
    firstName = fName;
    lastName = lName;
    major = maj;
    this.gpa = gpa;
}

Here's the add method from another class....

private static void addStudent(ArrayList<Student> L) {
    System.out.println();
    Scanner input = new Scanner(System.in);
    System.out.print("First name: ");
    String uFName = input.nextLine();
    System.out.print("Last name: ");
    String uLName = input.nextLine();
    System.out.print("Major: ");
    String studyMaj = input.nextLine();
    System.out.print("GPA: ");
    double grades = input.nextDouble();
    Student newStudent = new Student(uFName, uLName, studyMaj, grades);
    L.add(newStudent);
    input.close();
}
4

1 回答 1

0

尝试这个

public Student( String fName, String lName, String maj, double gpa,number ) {
firstName = fName;
lastName = lName;
major = maj;
this.gpa = gpa;
sNumber = number;
}
private static void addStudent(ArrayList<Student> L) {
System.out.println();
Scanner input = new Scanner(System.in);
System.out.print("First name: ");
String uFName = input.nextLine();
System.out.print("Last name: ");
String uLName = input.nextLine();
System.out.print("Major: ");
String studyMaj = input.nextLine();
System.out.print("GPA: ");
double grades = input.nextDouble();
int number=0;
if(l.size()>0){
  number=l.get(l.size()-1).getSNumber()+1; 
}
Student newStudent = new Student(uFName, uLName, studyMaj, grades,number);
L.add(newStudent);
input.close();
}
于 2013-09-06T06:00:07.383 回答