小程序交流群:528215729,加入和小伙伴们一起探讨开发疑难、运营idea吧

admin

admin

发表于 2018-05-13 14:11:03

摘要:给课程加入学员的小程序 (微信小程序开发 - 微信 小程序外包开发-微信小程序 开发教程-微信小程序开发者社区,尽在 www.mntuku.cn)

给指定课程加入学员的程序

代码如下:

第一部分
package Day06;

public class Course {
private String courseName;
private String[] students = new String [100];
private int numberOfStudents;

public Course(String courseName) {
this.courseName = courseName;
}

public void addStudent(String student) {
this.students[numberOfStudents] = student;
this.numberOfStudents++;
}

public String[] getStudents() {
return students;
}

public int getNumberOfStudents() {
return numberOfStudents;
}

public String getCourseName() {
return courseName;
}

public void dropStudent(String student) {
int i = 0;
while(i < numberOfStudents) {
if (student.equals(students[i])) {
break;
}
i++;
}
if (i < numberOfStudents) {
while (i < numberOfStudents) {
this.students[i] = this.students[i + 1];
i++;
}
numberOfStudents--;
this.students[numberOfStudents] = null;
}

}
}
第二部分
package Day06;

public class TestCourse {
public static void main(String[] args) {
Course course1 = new Course("Java Programming");
course1.addStudent("John");
course1.addStudent("Peter");
System.out.println("Number of Students is: " + course1.getNumberOfStudents());
for (int i = 0; i< course1.getNumberOfStudents(); i++) {
System.out.println("Student " + i
+ " is: " + course1.getStudents()[i]);
}

}
}

13阅读 | 0评论
你的回应