不知道哪错了
不知道哪里有错。明明传了3个学生,可是显示200个
package com.imooc.model;
/**
* 专业类
* @author imooc
*
*/
public class Subject {
// 成员属性:学科名称、学科编号、学制年限、报名选修的学生信息、报名选修的学生个数
private String subjectName;
private String subjectNo;
private int subjectlife;
private Student[] myStudents;
private int studentNum;
public int getStudentNum() {
return studentNum;
}
public void setStudentNum(int studentNum) {
this.studentNum = studentNum;
}
//获取选修专业的学生信息,如果保存学生信息的数组未被初始化,则先初始化长度200
public Student[] getMyStudents() {
if(this.myStudents==null) {
this.myStudents=new Student[200];
}
return myStudents;
}
public void setMyStudents(Student[] myStudents) {
this.myStudents = myStudents;
}
// 无参构造方法
public Subject() {
}
// 带参构造,实现对学科名称、编号、学制年限赋值
public Subject(String subjectName, String subjectNo, int subjectlife) {
this.setSubjectName(subjectName);
this.setSubjectNo(subjectNo);
this.setSubjectlife(subjectlife);
}
// 带参构造,实现对全部属性的赋值
public Subject(String subjectName, String subjectNo, int subjectlife,Student[] myStudents) {
this.setSubjectName(subjectName);
this.setSubjectNo(subjectNo);
this.setSubjectlife(subjectlife);
this.setMyStudents(myStudents);
}
public String getSubjectName() {
return subjectName;
}
public void setSubjectName(String subjectName) {
this.subjectName = subjectName;
}
public String getSubjectNo() {
return subjectNo;
}
public void setSubjectNo(String subjectNo) {
this.subjectNo = subjectNo;
}
public int getSubjectlife() {
return subjectlife;
}
//设置学制年限,限制必须大于0
public void setSubjectlife(int subjectlife) {
if (subjectlife <= 0)
return;
this.subjectlife = subjectlife;
}
/**
* 专业介绍的方法
* 专业介绍的相关信息:包括名称、编号、年限*/
public String info() {
String str="专业信息如下:\n专业名称:"+this.getSubjectName()+"\n专业编号:"+this.getSubjectNo()+"\n专业年限:"+this.getSubjectlife();
return str;
}
public void addStudent(Student stu) {
// 1.将学生保存到数组中
for(int i=0;i<this.getMyStudents().length;i++) {
if(this.getMyStudents()[i]==null) {
this.getMyStudents()[i]=stu;
// 2.将学生个数保存到studentNum
this.studentNum=i+1;
}
}
}
}
package com.imooc.model;
public class Student {
// 成员属性:学号、姓名、性别、年龄、专业
private String studentNo;
private String studentName;
private String studentSex;
private int studentAge;
private Subject studentSubject;
// 无参构造
public Student() {
super();
}
//多参构造方法,实现对学号、姓名、性别、年龄属性的赋值
public Student(String studentNo, String studentName, String studentSex, int studentAge) {
this.setStudentNo(studentNo);
this.setStudentName(studentName);
this.setStudentSex(studentSex);
this.setStudentAge(studentAge);
}
// 多参构造方法,实现对全部属性的赋值
public Student(String studentNo, String studentName, String studentSex, int studentAge,Subject studenSubject) {
this.setStudentNo(studentNo);
this.setStudentName(studentName);
this.setStudentSex(studentSex);
this.setStudentAge(studentAge);
this.setStudentSubject(studenSubject);
}
/**
* 获取专业对象,如果没有实例化,先实例化后再返回
* */
public Subject getStudentSubject() {
if(this.studentSubject==null) {
this.studentSubject=new Subject();
}
return studentSubject;
}
public void setStudentSubject(Subject studentSubject) {
this.studentSubject = studentSubject;
}
public String getStudentNo() {
return studentNo;
}
public void setStudentNo(String studentNo) {
this.studentNo = studentNo;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getStudentSex() {
return studentSex;
}
public void setStudentSex(String studentSex) {
// 限制性别只能是’男‘或者’女‘,否则为’男‘
if (studentSex.equals("男")) {
this.studentSex = studentSex;
} else if (studentSex.equals("女")) {
this.studentSex = studentSex;
} else {
this.studentSex = "我也不清楚";
}
}
public int getStudentAge() {
return studentAge;
}
/**
* 给年龄赋值,限定必须在10--100之间,反之赋值为18 studentAge 传入的年龄
*/
public void setStudentAge(int studentAge) {
if (studentAge < 10 || studentAge > 100) {
this.studentAge = 18;
} else {
this.studentAge = studentAge;
}
}
/**
* 学生自我介绍的方法 自我介绍的信息,包括姓名、学号、性别、年龄
*/
public String introduction() {
String str = "学生信息如下:\n姓名:" + this.getStudentName() + "\n学号:" + this.getStudentNo() + "\n性别:"
+ this.getStudentSex() + "\n年龄:" + this.getStudentAge()+ "\n所学专业:" + this.getStudentSubject().getSubjectName() + "\n学制年限:"
+ this.getStudentSubject().getSubjectlife();
return str;
}
/**
* 学生自我介绍的方法 subjectName subjectLife
*
* 自我介绍的信息,包括姓名、学号、性别、年龄、所学专业名称、学制年限
*/
public String introduction(String subjectName, int subjectLife) {
String str = "学生信息如下:\n姓名:" + this.getStudentName() + "\n学号:" + this.getStudentNo() + "\n性别:"
+ this.getStudentSex() + "\n年龄:" + this.getStudentAge() + "\n所学专业:" + subjectName + "\n学制年限:"
+ subjectLife;
return str;
}
/**
* 学生自我介绍的方法
* mySubject
* 自我介绍的信息,包括姓名、学号、性别、年龄、所学专业名称、学制年限
*/
public String introduction(Subject mySubject) {
String str = "学生信息如下:\n姓名:" + this.getStudentName() + "\n学号:" + this.getStudentNo() + "\n性别:"
+ this.getStudentSex() + "\n年龄:" + this.getStudentAge() + "\n所学专业:" + mySubject.getSubjectName()+ "\n学制年限:"
+mySubject.getSubjectlife();
return str;
}
}
package com.imooc.text;
import com.imooc.model.Student;
import com.imooc.model.Subject;
public class SchoolText {
public static void main(String[] args) {
//测试Subject
Subject sub1=new Subject("计算机科学与应用","J0001",3);
System.out.println(sub1.info());
System.out.println("=======================================");
//测试Student
Student stu1=new Student("S01","张三","男",200,sub1);
System.out.println(stu1.introduction());
System.out.println("=======================================");
Student stu2=new Student("S02","李石","女",17);
System.out.println(stu2.introduction("计算机科学与应用",3));
System.out.println("=======================================");
Student stu3=new Student("S03","万物","女",18);
System.out.println(stu3.introduction(sub1));
System.out.println("=======================================");
//测试指定专业中到底有多少学生报名
sub1.addStudent(stu1);
sub1.addStudent(stu2);
sub1.addStudent(stu3);
System.out.println(sub1.getSubjectName()+"中已经有"+sub1.getStudentNum()+"学生进行了报名");
}
}
正在回答
同学你好,因同学的数组初始化长度为200,则循环执行了200次,从而导致最后的结果为200,则建议同学在if判断中添加一个break,当符合条件时结束循环。如下图所示:
祝学习愉快!
- 参与学习 人
- 提交作业 9393 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星