我有一个主要课程说
Students registeredStudent = null;
我必须自己写学生课,因为没有给出。我似乎无法弄清楚'注册学生班级的学生价值。目前我的学生课程如下 -
public class Students {
String name;
String id;
//boolean selectedStudent;
public Students(String id, String name){
this.id = id;
this.name = name;
}
public void display(){
System.out.println("ID : " + id);
System.out.println("Name of Student :" + name);
}
}
公共类DrivingSchool {
private Autocar [] ac = new Autocar[10];
private Motorbike [] m = new Motorbike[10];
private Manualcar [] mc = new Manualcar[10];
private Students [] s = new Students[30];
public void start(){
String[]menu = {"Display all vehicles", "Display all students",
"Register a new student", "Assign student an auto car", "Assign student a manual car",
"Assign student a motorbike","Deregister a student"};
while(true){
int choice = Keyboard.readInt("Enter Your Choice >");
if(choice == 1){
displayAllV();
}
else if(choice == 2){
}
else if(choice == 3){
}
else if(choice == 4){
}
else if(choice == 5){
}
else if(choice == 6){
}
else if(choice == 7){
}
else{
break;
}
}
}
public void displayAllV(){
for(int i = 0;i < ac.length; i++){
if(ac[i] != null){
ac[i].display();
}
}
for(int j = 0; j<m.length; j++){
if(m[j] != null){
m[j].display();
}
}
for(int x = 0; x < mc.length; x++){
if(mc[x]!=null){
mc[x].display();
}
}
}
public void displayAllStud(){
boolean hasStud = false;
for(int i = 0;i < s.length; i++){
if(s[i] != null){
s[i].display();
hasStud = true;
}
}
if(!hasStud){
System.out.println("No Student Registered");
}
}
public void registerStud(){
String id = Keyboard.readString("Enter Student ID :");
for(int i = 0;i < s.length; i++){
if(s[i]!= null && id.equals(s[i].id)){
System.out.println("Student with this ID already exist");
return;
}
}
boolean registered = false;
for(int i =0;i < s.length; i++){
if(s[i] == null){
String name = Keyboard.readString("Enter Student name :");
s[i] = new Students(id, name);
System.out.println("A new student is registered.");
registered = true;
break;
}
}
if(!registered){
System.out.println("Unable to register. School is full");
}
}
public void assignAutoCar(){
String id = Keyboard.readString("Enter Student ID :");
Students selectedStudent = null;
for(int i =0; i<s.length; i++){
if(s[i] != null && id.equals(s[i].id)){
selectedStudent = s[i];
break;
}
}
if(selectedStudent != null && selectedStudent.autocar == null){
for(int i =0; i < ac.length; i++){
if(ac[i].isVehicleAvailable()){
ac[i].setAvail(false);
selectedStudent.autoCar = ac[i];
break;
}
}
public void createVehicles(){
ac[0] = new Autocar("LA1", 4);
ac[1] = new Autocar("LA2", 4);
ac[2] = new Autocar("LA3", 4);
ac[3] = new Autocar("LA4", 4);
ac[4] = new Autocar("LA5", 4);
mc[0] = new Manualcar("LM1", 4, 5);
mc[1] = new Manualcar("LM2", 4, 5);
mc[2] = new Manualcar("LM3", 4, 5);
mc[3] = new Manualcar("LM4", 4, 5);
mc[4] = new Manualcar("LM5", 4, 5);
m[0] = new Motorbike("M1", 2);
m[1] = new Motorbike("M2", 2);
m[2] = new Motorbike("M3", 3);
m[3] = new Motorbike("M4", 3);
m[4] = new Motorbike("M5", 4);
}
}
答案 0 :(得分:0)
首先,注册学生不属于您的学生班级。
这项任务:
Students registeredStudent = null;
主要方法中有哪些,说:嘿,我希望registeredStudent是一个局部变量,它是对学生类型(你的类)对象的引用,目前使它指向null。
您可以将registeredStudent指向实际的Students对象:
registeredStudent = new Students("1", "Mike");
现在,registeredStudent指向一个新的Students对象,其id为1,名称为Mike。
因此,registeredStudent不属于Students类。它只是main方法中的局部变量。
(注意:你最好用Singular形式命名你的课程:例如学生而不是学生。当你想创建一个新的学生对象时,新学生(......)比新学生更有意义(......) )
答案 1 :(得分:0)
在主要课程中,您必须在Student
课程的构造函数中传递已注册的学生ID和学生姓名,如:
Student registeredStudent = new Student (1,"Austin");
registeredStudent.display();
答案 2 :(得分:0)
registeredStudent只是Student类型的引用,其值为null。如果你想创建Student对象而不是改变你的Students类并在你的主类中写
Students registeredStudent = new Student("someId","someName");
//after this you can use your display() method
registeredStudent.display();