链接列表中的对象

时间:2016-04-10 16:19:47

标签: java object linked-list

我尝试编写一个带有一些选项的基本菜单的代码。这些选项有以下方法:AddStudent,changeName,setGrade等。我创建了一个名为Student的对象,它有一个名字,一个等级和一个年龄。我想在链接列表中添加学生但是当我使用该方法时添加它不起作用。这是我的代码:

import java.util.*;
class Student {  
    int age;
    int grade;
    String name;
    static LinkedList ll = new LinkedList();
    public Student (String n) {   //we create here a student with a name and an age
        name=n;
        age=0;
        grade=0;
    }
//-------------------------------------------------------------------------
    public void p(String x) {
        System.out.println(x);
    }

    public void addStudent() {
        Scanner s = new Scanner(System.in);
        p("Enter the name that you want");
        String f = s.nextLine();
        Student a = new Student(f);
        ll.add(a);
    }

    public void changeName() {                           //this method is to change the name of a student
        Scanner s = new Scanner(System.in);
        p("Enter whose name you want to change");  
        String c = s.nextLine();
        p("Enter the name that you want");
        String b = s.nextLine();

    }

    public void setGrade(Student a) {                 //this method is to put the student's grade
        Scanner s = new Scanner(System.in);
        p("Enter the grade that you want");
        int g = s.nextInt();
        a.grade=g;
    }

    public void setAge(Student a) {                 //This method is to put the student's grade
        Scanner s = new Scanner(System.in);
        p("Enter the age that you want");
        int h = s.nextInt();
        a.age=h;
    }

    public String getName(Student a) {
        return a.name;
    }

    public int getAge(Student a) {
        return a.age; 
    }

    public int getGrade(Student a) {
        return a.grade; 
    }
}

问题在于addStudent的方法。还有其他方法可以用来制作同一个项目吗?

2 个答案:

答案 0 :(得分:1)

从逻辑上思考这个问题。您有一个代表单个学生的Student课程。为什么学生会有学生名单?这毫无意义。

你不会有像Course这样的程序或者有学生名单的东西吗?这就是你的名单所属的地方。除非你有令人信服的理由(罕见),否则不要使用静电。

以下是Course课程的开始,该课程使用Student存储学生信息并将其存储在LinkedList Course内。您仍然需要实现findStudent方法,并且可能需要一种方法来打印List:

课程:

import java.util.LinkedList;
import java.util.Scanner;

public class Course {
    LinkedList ll = new LinkedList();
    Scanner s = new Scanner(System.in);

    public void addStudent() {
        p("Enter the name that you want");
        String f = s.nextLine();
        Student a = new Student(f);
        ll.add(a);
    }

    public void changeName() {                           //this method is to change the name of a student
        Student student = findStudent();
        p("Enter the name that you want");
        String newName = s.nextLine();
        //student.setName(newName);
    }

    public void setGrade() {                 //this method is to put the student's grade
        Student student = findStudent();
        p("Enter the grade that you want");
        int grade = s.nextInt();
        //student.setGrade(grade);
    }

    public void setAge() {                 //This method is to put the student's grade
        Student student = findStudent();
        p("Enter the age that you want");
        int age = s.nextInt();
       student.setAge(age);
    }

    public Student findStudent(){
        p("Which student did you want to change? Please enter their name:");
        String name = s.nextLine();
        //Find student in the list - left for the author
        Student student = null;
        return student;
    }

    //-------------------------------------------------------------------------
    public void p(String x) {
        System.out.println(x);
    }

    public static void main(String[] args) {
        Course course = new Course();
        course.addStudent();
        course.addStudent();
        course.changeName();
        course.setGrade();
    }
}

修改后的学生班:

import java.util.*;

public class Student {
    int age;
    int grade;
    String name;

    public Student (String n) {   //we create here a student with a name and an age
        name=n;
        age=0;
        grade=0;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setGrade(int grade) {
        this.grade = grade;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName(Student a) {
        return a.name;
    }

    public int getAge(Student a) {
        return a.age;
    }

    public int getGrade(Student a) {
        return a.grade;
    }

    @Override
    public String toString(){
        return "Student(name:" + name + ", age:" + age + ", grade:" + grade + ")";
    }
}

答案 1 :(得分:1)

这段代码有点乱,因为你包含了一些不应该出现在Student类中的代码。例如,addStudent方法不是静态的,为了调用它,您需要首先实例化Student的实例并在其上调用方法。但是,该方法试图要求用户输入新Student实例的信息,这是一个糟糕的设计。

所以,让Student类只做它应该做的事情。对于您的情况,Student只需要存储其年龄,等级和名称字段,并定义构造函数以初始化这些字段,以及可选的getter和setter方法,以根据您的需要设置和检索这些字段。

你需要一位经理'管理您的应用程序的类。该课程将跟踪学生列表,并要求用户输入新学生的信息,然后初始化学生实例并将其放入列表中。此Manager类甚至可以管理您需要用户输入或向用户显示信息的UI。因此,提供addStudent方法是该课程的责任。

Student课程本身应该对你的应用程序的逻辑一无所知,就像它可能是课程选择程序或其他东西。它只管理自己的信息,而某些经理类将负责应用程序的逻辑。