package 学生选课;import java.util.Set;import java.util.HashSet;public class student {private String num;private String name;private Set <lesson>lessons;public String getName(){ return name; }public void setName(String newName){ name=newName; }public String getNum(){ return num; }public void setNum(String newNum){ num=newNum;}public void setLessons(){ lessons=new HashSet<lesson>();}public Set getLessons(){ return lessons;}}package 学生选课;import java.util.ArrayList;import java.util.List;import java.util.Scanner;import java.util.Set;public class set { public List<lesson>choice=new ArrayList<lesson>(); public void testadd(){ lesson kechen=new lesson(); kechen.setName("篮球"); kechen.setNum("1"); choice.add(kechen); lesson temp=(lesson)choice.get(0); lesson kechen1=new lesson(); kechen1.setName("c语言"); kechen1.setNum("2"); choice.add(kechen1); lesson temp1=(lesson)choice.get(1); //System.out.println("课程添加成功:"+temp1.getName()+":"+temp1.getNum()); lesson kechen2=new lesson(); kechen2.setName("大学英语"); kechen2.setNum("3"); choice.add(kechen2); lesson temp2=(lesson)choice.get(2); // System.out.println("课程添加成功:"+temp2.getName()+":"+temp2.getNum()); lesson kechen3=new lesson(); kechen3.setName("微积分"); kechen3.setNum("4"); choice.add(kechen3); lesson temp3=(lesson)choice.get(3); //System.out.println("课程添加成功:"+temp3.getName()+":"+temp3.getNum()); child ch=new child(); ch.setNum("5"); ch.setName("语文"); choice.add(ch); lesson le =(lesson)choice.get(4); //System.out.println("我是子类型添加了!"+le.getNum()+"\t"+le.getName()); } public void foreach(){ for(lesson les:choice){ System.out.println(les.getNum()+"\t"+les.getName()); } } public static void main(String[] args) { set set=new set(); set.testadd(); set.foreach(); student stu=new student(); System.out.println("欢迎选课!"); Scanner res=new Scanner(System.in); for (int x=0;x<3;x++){ System.out.println("请输入课程序号:"); String num=res.next(); for(lesson le:set.choice){ if (le.getNum().equals(num)){ stu.getLessons(); stu.setLessons(); stu.getLessons().add(le); } } } for (lesson le : stu.getLessons()) {} }}最后这个for循环应该怎么写啊。我怎么写都是错的,要奔溃了!!!!!
添加回答
举报
0/150
提交
取消