gpt4 book ai didi

java - 使用 for 循环和用户输入将元素添加到 LinkedList

转载 作者:行者123 更新时间:2023-12-02 01:09:08 25 4
gpt4 key购买 nike

我正在尝试使用 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()+ "\n\n";
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 ,和GPAstatic ,这意味着可以从您创建的所有学生对象访问它们。因此,当您创建一个新的学生对象并调用它的构造函数时,您会更改 name 的值。 , address ,和GPA对于您之前创建的所有其他学生对象。

要解决您的问题,您需要删除 static name 声明中的关键字, address ,和GPA .

现在剩下的就是更改访问变量的方式。请注意您过去如何使用 Student.name每当您想使用属性name时?这仅在 name 时有效是静态的“又名 name 对于所有 Student 都是相同的”。我们现在要使用name 当前学生而不是所有学生,所以我们应该使用 this.name而不是Student.name 。同样改变Student.GPAthis.GPAStudent.addressthis.address .

此外,您不能只使用属性 name , address ,和GPA在你的 main 中没有对象“因为它们不再是 static”,所以你需要声明 name , address ,和GPA在 main 中,请注意这些变量与 Student 中的变量属性无关。类(class)。请参阅此代码以更好地理解,对于我糟糕的解释感到抱歉。

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()+ "\n\n";
return str;
}
}

关于java - 使用 for 循环和用户输入将元素添加到 LinkedList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59620202/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com