0

我正在尝试制作一个简单的程序来演示对象的 ArrayLists 的使用。我希望能够输入要存储在对象中的详细信息以及搜索和打印对象。

我正在为学生做这件事我有我的学生课程,我已经开始了我的学生测试课程,但我正在寻找输入的帮助。

这是我到目前为止所拥有的:

package student;

public class Student {

    private String studentName;
    private String studentNo;
    private String email;
    private int year;

    public Student() {
      /**studentName = null;
        *studentNo = null;
        *email = null;
        *year = -1;
        */
    }

    public Student(String nName, String nNum, String nEmail, int nYr) {
        this.studentName = nName;
        this.studentNo = nNum;
        this.email = nEmail;
        this.year = nYr;
    }

    public void setStudentName(String newStudentName) {
        studentName = newStudentName;
    }

    public void setStudentNo(String newStudentNo) {
        studentNo = newStudentNo;
    }

    public void setEmail(String newEmail) {
        email = newEmail;
    }

    public void setYear(int newYear) {
        year = newYear;
    }

    public String getStudentName() {
        return studentName;
    }

    public String getStudentNo() {
        return studentNo;
    }

    public String getEmail() {
        return email;
    }

    public int getYear() {
        return year;
    }


    }
}

package student;
import java.util.ArrayList;
import java.util.Scanner;

public class studentTest {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        Student student1 = new Student();
        Student student2 = new Student();
        Student student3 = new Student();
        Student student4 = new Student();
        Student student5 = new Student();


    }
}
4

3 回答 3

1

查看Scanner的文档。

从您到目前为止所做的代码studentTest看来,您正在获取用户的所有输入。

您基本上需要循环输入所有数据:

// create ArrayList
for (int i=0; i < 5; i++) {
   String studentName = input.nextLine();
   ....

   Student student = new Student();
   student.setStudentName();
   ...

   // add student to arraylist
}

我认为您将从填写我遗漏的部分中受益。

于 2012-10-15T20:59:19.850 回答
0

我认为他只是想填充学生类这样的东西。

 while(true){
    Scanner input = new Scanner(System.in);

    Student student1 = new Student();
    int year= input.nextInt();
    String studentName=input.nextLine();
    studentNo=input.nextLine();
    String email=input.nextLine();

    student1.setStudentName(studentName);
    student1.setStudentNo(studentNo);
    student1.setEmail(email);
    student1.setYear(year) 

    int year= input.nextInt();
    String studentName=input.nextLine();
    studentNo=input.nextLine();
    String email=input.nextLine();



      break; // at the last position
      }
于 2012-10-15T21:09:06.330 回答
-1

您可能需要更新 main 方法,如下所示:

public static void main(String[] args) {
    List<Student> students = new ArrayList<Student>();
    Scanner input = new Scanner(System.in);

    System.out.println("Enter number of students");
    int countStudents = input.nextInt();

    for(int i=0; i< countStudents; i++){
          Student student = new Student();
          System.out.println("Enter details for student"+i);

          System.out.println("Enter name");
          student.setStudentName(input.next()); 

          System.out.println("Enter Number");
          student.setStudentNo(input.next()); 

          System.out.println("Enter email");
          student.setEmail(input.next()); 

          System.out.println("Enter year");
          student.setYear(input.nextInt()); 
          students.add(student);
    }
    //Your list of students is populated now
}
于 2012-10-15T21:00:48.013 回答