【发布时间】:2016-04-10 16:19:46
【问题描述】:
我尝试编写一个包含一些选项的基本菜单的代码。这些选项有以下方法: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 的方法上。还有其他方法可以让我制作相同的项目吗?
【问题讨论】:
-
编译器有什么问题?
-
遇到什么错误?
标签: java object linked-list