正在回答
8回答
同学你好,应该将学生添加到输入的班级中。
修改代码如下:
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
KLovei
2020-05-08 16:57:24
package com.imooc.student; import java.util.List; import java.util.Scanner; import java.util.Set; // 主菜单 public void mainMenu() { System.out.println("****************************"); System.out.println(" **主菜单**"); System.out.println(" 1--班级管理"); System.out.println(" 2--学校管理"); System.out.println(" 0--退出"); System.out.println("****************************"); } // 学校管理菜单 public void schoolMenu() { System.out.println("***********************************************************"); System.out.println(" **学校管理**"); System.out.println(" 1--创建班级"); System.out.println(" 2--删除班级"); System.out.println(" 3--通过班级名称查询班级信息"); System.out.println(" 4--对各班语文成绩按平均分进行由大到小的排序"); System.out.println(" 5--对各班数学成绩按平均分进行由大到小的排序"); System.out.println(" 6--显示所有班级名称"); System.out.println(" 9--返回上一级菜单"); System.out.println("***********************************************************"); } // 班级管理菜单 public void banJiMenu() { System.out.println("***********************************************************"); System.out.println(" **班级管理**"); System.out.println(" 1--添加学生信息到主学生列表"); System.out.println(" 2--添加学生信息到普通班级"); System.out.println(" 3--通过学号查询学生信息"); System.out.println(" 4--输入班级的语文成绩"); System.out.println(" 5--输入班级的数学成绩"); System.out.println(" 6--删除学生信息"); System.out.println(" 7--显示所有学生信息"); System.out.println(" 9--返回上一级菜单"); System.out.println("***********************************************************"); } // 主流程 public void test() { TestDemo td = new TestDemo(); School school = new School(); Scanner sc = new Scanner(System.in); int input1 = 0, input2 = 0, input3 = 0; BanJi mainBanJi = new BanJi("B001", "主学生列表"); school.addBanJi(mainBanJi); BanJi BanJi1 = null; List<Student> list = null; while (true) { td.mainMenu(); System.out.println("请输入对应的数字进行操作:"); input1 = sc.nextInt(); if (input1 == 0) { break; } switch (input1) { case 1: while (true) { td.banJiMenu(); System.out.println("请输入对应的数字对班级进行管理"); input2 = sc.nextInt(); if (input2 == 9) { break; } switch (input2) { case 1: System.out.println("添加学生信息到主播放列表:"); System.out.println("情输入要添加的学生信息个数:"); int count1 = sc.nextInt(); for (int i = 1; i <= count1; i++) { System.out.println("请输入第" + i + "个学生的信息:"); System.out.println("请输入学生id:"); String id = sc.next(); System.out.println("请输入学生姓名:"); String name = sc.next(); Student stu = new Student(id, name); mainBanJi.addStudent(stu); } mainBanJi.displayBanJiName(); break; case 2: System.out.println("添加学生信息到普通班级:"); System.out.println("请输入要添加的班级名称:"); String strName = sc.next(); BanJi banji = school.searchByName(strName); if (banji == null) { System.out.println("该班级不存在!"); break; } else { System.out.println("请输入要添加的学生信息个数:"); int count2 = sc.nextInt(); for (int i = 1; i <= count2; i++) { System.out.println("请输入第" + i + "个学生的信息:"); System.out.println("请输入学生id:"); String id = sc.next(); Student stu = mainBanJi.searchStudentByNum(id); if (stu == null) { System.out.println("该学生信息在主学生列表中不存在,请输入学生的其他信息:"); System.out.println("请输入要添加的学生姓名:"); String name = sc.next(); Student stu1 = new Student(id, name); BanJi1.addStudent(stu1); mainBanJi.addStudent(stu1); school.addBanJi(BanJi1); } else { BanJi1.addStudent(stu); System.out.println("添加成功!"); } } BanJi1.displayBanJiName(); } break; case 3: System.out.println("通过学号查询学生信息:"); System.out.println("请输入要查询的班级信息:"); String name = sc.next(); BanJi banji1 = school.searchByName(name); if (banji1 == null) { System.out.println("该班级不存在"); } else { System.out.println("请输入要查询的学生学号"); String num = sc.next(); if (banji1.searchStudentByNum(num) == null) { System.out.println("该学生信息不存在"); } else { System.out.println(banji1.searchStudentByNum(num)); } } break; case 4: System.out.println("输入班级的语文成绩:"); System.out.println("请选择要输入的班级名称:"); String name1 = sc.next(); BanJi banji3 = school.searchByName(name1); if (banji3 == null) { System.out.println("该班级不存在!"); } else { for (Student stu : banji3.getStuList()) { if (stu != null) { for (int i = 1; i <= banji3.getStuList().size(); i++) { System.out.println(banji3.getStuList().get(i)); System.out.println("请输入学生的语文成绩"); int score = sc.nextInt(); banji3.insertChineseScore(banji3.getStuList().get(i).getStuNum(), score); } } else { System.out.println("该班级没有学生"); } } } break; case 5: System.out.println("输入班级的数学成绩:"); System.out.println("请选择要输入的班级名称:"); String name3 = sc.next(); BanJi b = school.searchByName(name3); if (b == null) { System.out.println("该班级不存在!"); } else { for (Student stu : b.getStuList()) { if (stu != null) { for (int i = 1; i <= b.getStuList().size(); i++) { System.out.println(b.getStuList().get(i)); System.out.println("请输入学生的数学成绩:"); int score1 = sc.nextInt(); b.insertMathScore(b.getStuList().get(i).getStuNum(), score1); } } else { System.out.println("该班级没有学生"); } } } break; case 6: System.out.println("删除学生信息:"); System.out.println("请选择要删除学生的班级名称:"); String name4 = sc.next(); BanJi banji5 = school.searchByName(name4); if (banji5 == null) { System.out.println("该班级不存在!"); } else { System.out.println("请输入要删除的学生学号:"); String stuName = sc.next(); banji5.deleteStudent(stuName); } break; case 7: System.out.println("显示所有学生信息:"); System.out.println("请输入要查询的班级名称:"); String name2 = sc.next(); BanJi banji4 = school.searchByName(name2); if (banji4 == null) { System.out.println("该班级不存在!"); } else { banji4.displayBanJiName(); } break; default: System.out.println("该数字没有对应的操作!"); break; } } break; case 2: while (true) { td.schoolMenu(); System.out.println("请输入对应的数字对班级进行管理"); input3 = sc.nextInt(); if (input3 == 9) { break; } switch (input3) { case 1: System.out.println("创建班级:"); System.out.println("请输入班级名称:"); String name = sc.next(); BanJi banji = school.searchByName(name); if (banji == null) { System.out.println("请输入班级编号:"); String num = sc.next(); BanJi1 = new BanJi(num, name); school.addBanJi(BanJi1); System.out.println("添加成功"); } else { System.out.println("该班级已存在,请勿重复创建!"); } break; case 2: System.out.println("删除班级:"); System.out.println("请输入要删除的班级名称:"); String className = sc.next(); BanJi banji6 = school.searchByName(className); if (banji6 == null) { System.out.println("该班级不存在"); } else if (className.equals("主学生列表")) { System.out.println("主学生列表不能删除!"); } else { school.deleteBanJi(banji6); System.out.println("删除成功!"); } break; case 3: System.out.println("通过班级名称查询班级信息:"); System.out.println("请输入要查询的班级名称:"); String className1 = sc.next(); BanJi banji7 = school.searchByName(className1); if (banji7 == null) { System.out.println("该班级不存在!"); } else { banji7.displayBanJiName(); } break; case 4: System.out.println("对各班语文成绩按平均分进行由大到小排序:"); Set<String> classSet = school.getSchoolMap().keySet(); boolean flag = false; for (String s : classSet) { if (s.equals("主学生列表")) { continue; } else { list = school.getSchoolMap().get(s).getStuList(); for (Student stu : list) { if (stu != null) { flag = true; break; } } } } if (flag) { school.sortChineseByAverage(); } else { System.out.println("还未向班级中添加学生"); } break; case 5: System.out.println("对各班数学成绩按平均分进行由大到小排序:"); school.sortMathByAverage(); break; case 6: System.out.println("显示所有班级名称:"); school.displayBanJiName(); break; default: System.out.println("该数字没有对应的操作!"); } } break; default: System.out.println("没有该数字对应的操作!"); break; } } } public static void main(String[] args) { TestDemo td = new TestDemo(); td.test(); } }
KLovei
2020-05-08 16:56:40
package com.imooc.student; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; public class School { private Map<String,BanJi> schoolMap; public School(){ schoolMap=new HashMap<String,BanJi>(); } public Map<String, BanJi> getSchoolMap() { return schoolMap; } public void setSchoolMap(Map<String, BanJi> schoolMap) { this.schoolMap = schoolMap; } /** * 添加班级 * @param banji 要添加的班级对象 */ public void addBanJi(BanJi banji){ schoolMap.put(banji.getClassName(),banji); } /** * 删除班级 * @param banji 要删除的班级对象 */ public void deleteBanJi(BanJi banji){ schoolMap.remove(banji.getClassName()); } /** * 通过班级名称查询班级 * @param className 要查询的班级名称 * @return 查询到的班级对象 */ public BanJi searchByName(String className){ BanJi banji=null; Set<String> classSet=schoolMap.keySet(); for(String s:classSet){ if(s.equals(className)){ banji=schoolMap.get(s); break; } } return banji; } /** * 对各班语文成绩按平均分进行由大到小的排序 */ public void sortChineseByAverage(){ Map<String, Float> score = new HashMap<String, Float>();//定义一个Map,key存放班级名称,value存放平均成绩,假设变量名为schoolMap BanJi banJi = null; List<Student> stuList = null; float sum = 0.0f; float average = 0.0f; // 求各班平均分 // 得到所有班对应的班级名称,根据名称得到班级对象 Set<String> nameSet = schoolMap.keySet();//取得班级列表中所有key值 //通过遍历所有key值检测班级列表中是否有班级,没有则输出"该班级没有学生!" if (nameSet != null) { //遍历班级列表中的key for (String s : nameSet) { //如果是主列表则跳出当前循环 if(s.equals("主学生列表"))continue; sum = 0.0f; //根据key值获得value对象,取得学生对象 banJi = schoolMap.get(s); //根据学生对象获取学生列表赋值stuList stuList = banJi.getStuList(); //判断学生列表是否为空 if (stuList != null) { //遍历该班所有的学生信息 for (Student stu : stuList) { //获得该班所有学生的语文成绩总和 sum = sum + stu.getChinese(); } // 获取列表大小,求平均分 average = sum / stuList.size(); // 将每个班级的平均分添加到列表score中 score.put(banJi.getClassName(),average); } else { System.out.println("该班级没有学生!"); } } } else { System.out.println("还未添加班级!"); } // 输出排序后的学生成绩(提供排序的代码) // 先将Map转换到List中,也就是将Map的Entry存放的List中 List<Map.Entry<String, Float>> list = new ArrayList<Map.Entry<String, Float>>(score.entrySet()); // 按照Map的Value值进行排序 Collections.sort(list, new Comparator<Map.Entry<String, Float>>() { // 降序排序 @Override public int compare(Entry<String, Float> o1, Entry<String, Float> o2) { // return o1.getValue().compareTo(o2.getValue()); return o2.getValue().compareTo(o1.getValue()); } }); for (Map.Entry<String, Float> mapping : list) { System.out.println(mapping.getKey() + "语文的平均分为" + mapping.getValue()); } } /** * 对各班数学成绩按平均分进行由大到小的排序 */ public void sortMathByAverage(){ Map<String, Float> score = new HashMap<String, Float>();//定义一个Map,key存放班级名称,value存放平均成绩,假设变量名为schoolMap BanJi banJi = null; List<Student> stuList = null; float sum = 0.0f; float average = 0.0f; // 求各班平均分 // 得到所有班对应的班级名称,根据名称得到班级对象 Set<String> nameSet = schoolMap.keySet();//取得班级列表中所有key值 //通过遍历所有key值检测班级列表中是否有班级,没有则输出"该班级没有学生!" if (nameSet != null) { for (String s : nameSet) { if(s.equals("主学生列表"))continue; sum = 0.0f; banJi = schoolMap.get(s); stuList = banJi.getStuList(); if (stuList != null) { for (Student stu : stuList) { sum = sum + stu.getMath(); } average = sum / stuList.size(); score.put(banJi.getClassName(), average); } else { System.out.println("该班级没有学生!"); } } } else { System.out.println("还未添加班级!"); } // 输出排序后的学生成绩(提供排序的代码) // 先将Map转换到List中,也就是将Map的Entry存放的List中 List<Map.Entry<String, Float>> list = new ArrayList<Map.Entry<String, Float>>(score.entrySet()); // 按照Map的Value值进行排序 Collections.sort(list, new Comparator<Map.Entry<String, Float>>() { // 降序排序 @Override public int compare(Entry<String, Float> o1, Entry<String, Float> o2) { // return o1.getValue().compareTo(o2.getValue()); return o2.getValue().compareTo(o1.getValue()); } }); for (Map.Entry<String, Float> mapping : list) { System.out.println(mapping.getKey() + "数学的平均分为:" + mapping.getValue()); } } /** * 显示所有班级名称 */ public void displayBanJiName(){ Set<String> keySet=schoolMap.keySet(); for(String s:keySet){ System.out.println(s); } } public void addBanJi(){ } }
KLovei
2020-05-08 16:56:24
package com.imooc.student; import java.util.ArrayList; import java.util.List; public class BanJi { private String classld;//班级编号 private String className;//班级名称 private List<Student> stuList;//存放学生的集合 public BanJi(){ } public BanJi(String classld, String className) { this.setClassld(classld); this.setClassName(className); stuList=new ArrayList<Student>(); } public String getClassld() { return classld; } public void setClassld(String classld) { this.classld = classld; } public String getClassName() { return className; } public void setClassName(String className) { this.className = className; } public List<Student> getStuList() { return stuList; } public void setStuList(List<Student> stuList) { this.stuList = stuList; } /** * 将学生存放到班级 * @param stu 要存放的学生信息 */ public void addStudent(Student stu){ boolean flag=false; for(Student s:stuList){ if(s.equals(stu)){ flag=true; break; } } if(flag){ System.out.println("该学生信息已存在,添加失败!"); }else{ stuList.add(stu); } } /** * 通过学号查询学生在班级中是否存在 * @param stuNum 查询的学号 * @return 查询到的学生对象 */ public Student searchStudentByNum(String stuNum){ Student student=null; for(Student s:stuList){ if(s.getStuNum().equals(stuNum)){ student=s; break; } } return student; } /** * 输入班级学生的语文成绩 * @param stuNum 学号 * @param score 成绩 */ public void insertChineseScore(String stuNum,float score){ Student stu=searchStudentByNum(stuNum); if(stu==null){ System.out.println("该学生信息不存在!"); }else{ stu.setChinese(score); System.out.println("添加语文成绩成功!"); } } /** * 输入班级学生的数学成绩 * @param stuNum 学号 * @param score 成绩 */ public void insertMathScore(String stuNum,float score){ Student stu=searchStudentByNum(stuNum); if(stu==null){ System.out.println("该学生信息不存在"); }else{ stu.setMath(score); System.out.println("添加数学成绩成功"); } } /** * 删除学生信息 * @param stuNum 要删除的学号 */ public void deleteStudent(String stuNum){ Student stu=searchStudentByNum(stuNum); if(stu==null){ System.out.println("该学生信息不存在"); }else{ stuList.remove(stu); System.out.println("删除成功"); } } /** * 显示所有学生的信息(包括学号和姓名) */ public void displayBanJiName(){ System.out.println("班级信息列表:"); for(Student stu:stuList){ System.out.println(stu); } } }
KLovei
2020-05-08 16:56:05
package com.imooc.student; public class Student { private String stuNum;// 学号 private String stuName;// 姓名 private float math;// 数学成绩 private float chinese;// 语文成绩 // 无参构造 public Student() { } // 带参构造,对全部属性赋值 public Student(String stuNum, String stuName, float math, float chinese) { this.setStuNum(stuNum); this.setStuName(stuName); this.setMath(math); this.setChinese(chinese); } //双参构造,对两个属性赋值 public Student(String stuNum, String stuName) { this.setStuNum(stuNum); this.setStuName(stuName); } public String getStuNum() { return stuNum; } public void setStuNum(String stuNum) { this.stuNum = stuNum; } public String getStuName() { return stuName; } public void setStuName(String stuName) { this.stuName = stuName; } public float getMath() { return math; } public void setMath(float math) { this.math = math; } public float getChinese() { return chinese; } public void setChinese(float chinese) { this.chinese = chinese; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + Float.floatToIntBits(chinese); result = prime * result + Float.floatToIntBits(math); result = prime * result + ((stuName == null) ? 0 : stuName.hashCode()); result = prime * result + ((stuNum == null) ? 0 : stuNum.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj.getClass() == Student.class) { Student stu = (Student) obj; return this.getStuNum().equals(stu.getStuNum()) && this.getStuName().equals(stu.getStuName()) && this.getMath() == stu.getMath() && this.getChinese() == stu.getChinese(); } else { return false; } } public String toString(String a) { return "[学号:" + stuNum + ", 姓名:" + stuName + ", 数学成绩:" + math + ", 语文成绩:" + chinese + "]"; } @Override public String toString() { return "[学号:" + stuNum + ", 姓名:" + stuName + "]"; } }
1. Java 零基础入门
- 参与学习 人
- 提交作业 3802 份
- 解答问题 11489 个
本阶段带你迈入Java世界,学习Java必备基础知识,基础语法、面向对象思想以及常用工具类的使用。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星