簡體   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