我正在尝试使用 for 循环将元素添加到 LinkedList 中,每当我打印列表时,它只包含我的一个输入,并将其放在每个索引中。
我有一种感觉,问题出在我的 toString 方法或我的 Student 构造函数上,但似乎无法弄清楚。
感谢任何和所有的帮助。谢谢!
import java.util.*;
public class Student {
private static String name;
private static String address;
private static double GPA;
static LinkedList<Student> stu = new LinkedList<Student>();
static Scanner scanner = new Scanner(System.in);
public Student(String name, String address, double GPA) {
Student.name = name;
Student.address = address;
Student.GPA = GPA;
}
public String getName() {
return Student.name;
}
public String getAddr() {
return Student.address;
}
public double getGPA() {
return Student.GPA;
}
public static void main(String [] args) {
for (int i = 0; i <= 2; i++) {
System.out.println("Enter the student's name: ");
name = scanner.next();
System.out.println("Enter the student's address: ");
address = scanner.next();
System.out.println("Enter the student's GPA: ");
GPA = scanner.nextDouble();
stu.addLast(new Student(name, address, GPA));
}
System.out.println(stu);
}
@Override
public String toString() {
String str = "Name: " + getName() + "nAddress: " + getAddr() + "nGPA: " + getGPA()+ "nn";
return str;
}
}
安慰
Enter the student's name:
Jim
Enter the student's address:
111Ave
Enter the student's GPA:
2.3
Enter the student's name:
Joe
Enter the student's address:
222Ave
Enter the student's GPA:
3.0
Enter the student's name:
Jack
Enter the student's address:
333Ave
Enter the student's GPA:
3.4
[Name: Jack
Address: 333Ave
GPA: 3.4
, Name: Jack
Address: 333Ave
GPA: 3.4
, Name: Jack
Address: 333Ave
GPA: 3.4
]
属性name
、address
和GPA
是static
的,这意味着可以从您创建的所有学生对象访问它们。因此,当您创建一个新的学生对象并调用它的构造函数时,您可以更改之前创建的所有其他学生对象的name
、address
和GPA
的值。
要解决您的问题,您需要从name
、address
和GPA
的声明中删除static
关键字。
现在剩下的就是改变你访问变量的方式。请注意,每当您想使用属性name
时,您如何使用Student.name
?这仅在name
是静态的"akaname
对所有Student
s都相同"时才有效。我们现在想使用当前学生的name
而不是所有学生,所以我们应该使用this.name
而不是Student.name
。同样,将Student.GPA
更改为this.GPA
,将Student.address
更改为this.address
。
此外,你不能只使用属性name
、address
和GPA
在 main 中没有对象"因为它们不再static
",所以你需要在 main 中声明name
、address
和GPA
,请注意这些变量与Student
类中的变量属性无关。 请参考此代码以更好地理解,对不起我的糟糕解释。
import java.util.*;
public class Student {
private String name;
private String address;
private double GPA;
static LinkedList<Student> stu = new LinkedList<Student>();
static Scanner scanner = new Scanner(System.in);
public Student(String name, String address, double GPA) {
this.name = name; //this.name instead of Student.name
this.address = address; //this.address instead of Student.address
this.GPA = GPA; //this.GPA instead of Student.GPA
}
public String getName() {
return this.name; //similarly
}
public String getAddr() {
return this.address; //similarly
}
public double getGPA() {
return this.GPA; //similarly
}
public static void main(String [] args) {
for (int i = 0; i <= 2; i++) {
System.out.println("Enter the student's name: ");
//notice here. "name" can be changed to anything, "sname" for example
//this variable is just to store the input, it's not related to the name
//attribute in the class
String name = scanner.next();
System.out.println("Enter the student's address: ");
//same goes for address and GPA
String address = scanner.next();
System.out.println("Enter the student's GPA: ");
double GPA = scanner.nextDouble();
//use the input taken above to create a new student by calling the constructor
stu.addLast(new Student(name, address, GPA));
}
System.out.println(stu);
}
@Override
public String toString() {
String str = "Name: " + getName() + "nAddress: " + getAddr() + "nGPA: " + getGPA()+ "nn";
return str;
}
}