繁体   English   中英

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

[英]adding elements to LinkedList using for loop and user input

我正在尝试使用 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

]

属性nameaddressGPAstatic ,这意味着可以从您创建的所有学生对象访问它们。 因此,当您创建一个新的 student 对象并调用它的构造函数时,您会更改之前创建的所有其他 student 对象的nameaddressGPA的值。

要解决您的问题,您需要从nameaddressGPA的声明中删除static关键字。

现在剩下的就是改变访问变量的方式。 请注意,每当您想使用属性name时,您是如何使用Student.name 这仅在name是静态的“aka name is the same for all Student s”时才有效。 我们现在想使用当前学生的name而不是所有学生,所以我们应该使用this.name而不是Student.name 同样将Student.GPA更改为this.GPA并将Student.address更改为this.address

此外,您不能只在 main 中使用属性nameaddressGPA而没有对象“因为它们不再是static ”,因此您需要在 main 中声明nameaddressGPA ,请注意这些变量与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()+ "\n\n";
        return str;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM