我正在制作一个程序,该程序创建一个名为 Student 的新类,该类设置并获取一些不同的变量。除了尝试将正确打印出来的数组元素存储到我的 ArrayList 中之外,我的一切基本上都可以正常工作,而是得到哈希码。我确信改变一两个小事情会解决它,但我无能为力。任何和所有建议表示赞赏,谢谢!
主要的:
Student st = new Student();
Student();
}
public static void Student() {
System.out.println("Enter the data for student 1 :");
Student st = new Student();
Scanner keyboard = new Scanner(System.in);
String name = keyboard.next();
double GPA = keyboard.nextDouble();
boolean enrolled = keyboard.nextBoolean();
String date = keyboard.next();
for (int i = 0; i<5; i++){
Student newStudent = new Student();
newStudent.setName(name);
newStudent.setGPA(GPA);
newStudent.setEnrolled(enrolled);
newStudent.setDate(date);
ArrayList <Student> strList = new ArrayList<Student>();
Student student[] = new Student[5];
newStudent.getName();
newStudent.getGPA();
newStudent.getEnrolled();
newStudent.getDate();
newStudent.toString();
student[i] = newStudent;
ArrayList myList = new ArrayList();
myList.add(student[i]);
System.out.println(student[i].getName());
System.out.print(student[i].getGPA());
System.out.print(student[i].getEnrolled());
System.out.print(student[i].getDate());
System.out.println(myList);
}
}
学生班:
public class Student {
private String name;
private double gpa;
private boolean enrolled;
private String date;
public Student() {
this.name = "";
this.gpa = 0.0;
this.enrolled = false;
this.date = "none";
}
public Student(String name1) {
this.name = name1;
this.gpa = 0.0;
this.enrolled = false;
this.date = "none";
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public void setGPA(double gpa) {
this.gpa = gpa;
}
public double getGPA() {
return this.gpa;
}
public void setEnrolled(boolean enrolled) {
this.enrolled = enrolled;
}
public boolean getEnrolled() {
return this.enrolled;
}
public void setDate(String date) {
this.date = date;
}
public String getDate(){
return this.date;
}
}