我只能插入一个数学成绩和一个语文成绩
来源:4-8 项目作业
慕运维lh
2019-07-31 22:50:37
package com.imooc.studentmanagementsystem;
public class Student {
private String stuId;
private String stuName;
private float stuMath;
private float stuChinese;
//无参构造
public Student()
{
}
//带参构造
public Student(String stuId,String stuName)
{
this.stuId = stuId;
this.stuName = stuName;
}
public String getStuId() {
return stuId;
}
public void setStuId(String stuId) {
this.stuId = stuId;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public float getStuMath() {
return stuMath;
}
public void setStuMath(float stuMath) {
this.stuMath = stuMath;
}
public float getStuChinese() {
return stuChinese;
}
public void setStuChinese(float stuChinese) {
this.stuChinese = stuChinese;
}
//重写toString方法
/*
* 根据题分析可得。学生信息输出的情况分为四种;即以下四种情况;
* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
if(this.getStuChinese()!=0 && this.getStuMath()==0) {
return "学生信息 [学生学号:" + stuId + ", 学生姓名:" + stuName + ",数学成绩:"+stuMath+"]";
}
else if(this.getStuMath()!=0 && this.getStuChinese()==0)
{
return "学生信息 [学生学号:" + stuId + ", 学生姓名:" + stuName + ",语文成绩:"+stuChinese+"]";
}
else if(this.getStuChinese()==0 && this.getStuMath() == 0)
{
return "学生信息 [学生学号:" + stuId + ", 学生姓名:" + stuName + "]";
}
else
return "学生信息 [学生学号:" + stuId + ", 学生姓名:" + stuName + " , 数学成绩:"+stuChinese+"语文成绩:"+stuMath+"]";
}
//重写hashCode方法
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + Float.floatToIntBits(stuChinese);
result = prime * result + ((stuId == null) ? 0 : stuId.hashCode());
result = prime * result + Float.floatToIntBits(stuMath);
result = prime * result + ((stuName == null) ? 0 : stuName.hashCode());
return result;
}
@Override
/*
* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
* 1、equals判断的是对象是否相等;this代表包括当前对象里所有属性;
* 2、对象如果没有实例化则直接为false
* 3、如果对象返回值是否是同一类对象;如果不是必须要强制转换类型;
*/
public boolean equals(Object obj) {
if(this==obj)
return true;
if(obj==null)
return false;
if(obj.getClass()!=Student.class)
{
Student student = (Student)obj;
return (student.getStuId().equals(stuId)&&student.getStuName().equals(stuName)&&student.getStuChinese()==stuChinese&&student.getStuMath()==stuMath);
}
else
return false;
}
}
package com.imooc.studentmanagementsystem;
import java.util.ArrayList;
import java.util.List;
public class BanJi {
private List<Student> StudentList;
private String mainBanjiName;
//无参构造;
public BanJi()
{
}
//带参构造传入的参数为班级名称
public BanJi(String mainBanjiName)
{
this.mainBanjiName = mainBanjiName;
StudentList = new ArrayList<Student>();
}
/*
* 将新的学生信息添加到学生信息集合中去;
* 1、判断集合中是否存在该学生,存在,不添加,不存在添加。
*/
public void addToStudentList(Student student)
{
//boolean flag = false;
for(Student n:StudentList)
{
if(n.equals(student))
{
System.out.println("该学生信息已经存在无法添加");
break;
}
}
StudentList.add(student);
}
//显示集合中所有元素;
public void displayStudentList()
{
for(Student n:StudentList)
{
System.out.println(n);
}
}
//通过学号查找学生信息
public Student searchStudentByStuId(String id)
{
for(Student n:StudentList)
{
if(n.getStuId().equals(id))
return n;
else
System.out.println("没有找到该学号信息的学生");
}
return null;
}
/*
* 输入班级的学生语文成绩
* 1查找是否有这个对象
* 2通过setChinese方法将这个分数传入进去
*/
public void insertChineseScore(String stuId,float score)
{
for(Student n1:StudentList)
{
if(n1.getStuId().equals(stuId))
{
n1.setStuChinese(score);
break;
}
else
{
System.out.println("没有查到到这个学号的学生。所以无法添加语文成绩咯。");
break;
}
}
}
//插入数学成绩
public void insertMathScore(String stuId,float score)
{
for(Student n2:StudentList)
{
if(n2.getStuId().equals(stuId))
{
n2.setStuMath(score);
break;
}else
{
System.out.println("没有查到到这个学号的学生。所以无法添加数学成绩咯。");
break;
}
}
}
/*
*删除学生信息
*1、因为数据传入是一个属性。而删除是整个对象的全部属性
*2、所以中途需要用一个对象,来接受这个属性对应的对象并且要判断这个对象是否存在
*3、最后可以直接用List对应的remove对应的方法移除这个元素;
*/
public void deleteinformation(String stuId)
{
Student stu1 = searchStudentByStuId(stuId);
if(stu1!=null)
{
StudentList.remove(stu1);
System.out.println("删除成功");
}else
System.out.println("没有查到该歌曲的信息。所以无法删除哦!");
}
public List<Student> getStudentList() {
return StudentList;
}
public void setStudentList(List<Student> studentList) {
StudentList = studentList;
}
public String getMainBanjiName() {
return mainBanjiName;
}
public void setMainBanjiName(String mainBanjiName) {
this.mainBanjiName = mainBanjiName;
}
}
package com.imooc.studentmanagementsystem;
public class TestDemo {
public void testStudent()
{
Student stu1 = new Student("001","王海林");
Student stu2 = new Student("003","陈奕迅");
System.out.println(stu1);
System.out.println(stu2);
}
public void testBanji()
{
Student stu1 = new Student("004","王海林");
Student stu2 = new Student("003"," 陈奕迅");
BanJi mainBanJi = new BanJi("主班级");
mainBanJi.addToStudentList(stu1);
mainBanJi.addToStudentList(stu2);
mainBanJi.insertChineseScore("004",90f);
mainBanJi.insertMathScore("004", 77f);
mainBanJi.insertChineseScore("003",90f);
mainBanJi.insertMathScore("003",90f);
mainBanJi.displayStudentList();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
TestDemo a = new TestDemo();
//a.testStudent();
a.testBanji();
}
}我通过一步一步看看函数运行的步骤始终没想通为什么第二次运行插入函数的时候增强型for循环中直接跳到else语句中了。希望老师也能给我讲一下for循环用一次之后 n的值有如何变化

1回答
同学你好,在添加语文成绩insertChineseScore方法中,for循环只执行了一遍!

修改建议如下,首先通过学号查询学生信息,当stu不为null时,表示学生存在,修改该学生的语文成绩!否则直接输出这个学生不存在,例如

同上修改,添加数学成绩的方法!
2、for循环用一次之后,会继续执行循环,但是贴出代码中for循环在第一次执行后的if和else中都直接使用break跳出循环,所以执行一次后,for循环就会结束!
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
相似问题