gpt4 book ai didi

java - 链接列表中的对象

转载 作者:行者123 更新时间:2023-12-02 03:43:45 25 4
gpt4 key购买 nike

我尝试编写一个代码,其中有一个带有一些选项的基本菜单。这些选项有以下方法: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方法上。还有其他方法可以用来制作相同的项目吗?

最佳答案

从逻辑上思考这个问题。您有一个代表单个学生的 Student 类。为什么学生会有学生名单?这没有任何意义。

您是否没有一个类似Course 的程序或可以保存学生列表的程序?那就是你的列表所属的地方。除非有令人信服的理由(罕见),否则不要使用静态。

这里是 Course 类的开始,它使用 Student 来存储学生信息,并将其存储在 LinkedList 中的 类(class)。您仍然需要实现 findStudent 方法,并且可能还需要实现打印列表的方法:

类(class):

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();
}
}

修改后的Student类:

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 + ")";
}
}

关于java - 链接列表中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36532271/

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