问答详情
源自:6-1 学生选课---判断 List 中课程是否存在

为什么我解析不了方法

https://img.mukewang.com/5c88c4c50001038908060728.jpg

是照着老师打的  只不过我是在idea上  这应该没关系的把

提问者:慕容9086035 2019-03-13 16:53

个回答

  • Pikachu2016
    2019-08-13 12:58:05

    感觉最后多了个 }

  • 慕慕3524473
    2019-04-10 23:24:37

    你把testForEachForSet方法放到main方法前面试试

  • 慕容9086035
    2019-03-13 17:29:32

    package com.test.collection;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    import java.util.Scanner;
    
    public class SetTest {
    
        public List<Course> coursesToSelect;
        //构造器
        public SetTest(){
            coursesToSelect=new ArrayList<Course>();
        }
    
    /*    public static class List_Test {
            //用于存放备选课程的List
            public List coursesToSelect;
    
            public List_Test(){
                this.coursesToSelect=new ArrayList();
            }
            }*/
            //用于网coursesToSelet中添加备选课程
            public void testAdd(){
                //创建一个课程对象,并通过调用add方法,添加到备选课程中
                Course cr1=new Course("1","数据结构");
                coursesToSelect.add(cr1);
                Course temp= (Course) coursesToSelect.get(0);
                //System.out.println("添加了课程:"+temp.id+";"+temp.name);
    
    
                Course cr2=new Course("2","C语言");
                coursesToSelect.add(0,cr2);
                Course temp2= (Course) coursesToSelect.get(0);
                //System.out.println("添加了课程:"+temp2.id+";"+temp2.name);
    
                //coursesToSelect.add(cr1);
                //Course temp0= (Course) coursesToSelect.get(2);
                //System.out.println("添加了课程:"+temp.id+";"+temp.name);
    
                //下面方法会抛出数组下标越界异常
                // Course cr3=new Course("3","test");
                //coursesToSelect.add(4,cr3);
    
                Course[] courses={new Course("3","离散数学"),new Course("4","汇编语言")};
                coursesToSelect.addAll(Arrays.asList(courses));
                Course temp3= (Course) coursesToSelect.get(3);
                Course temp4= (Course) coursesToSelect.get(4);
    
                //System.out.println("添加了两门课程:"+temp3.id+";"+temp.name+";"+temp4.id+";"+temp4.name);
    
                Course[] courses2={new Course("5","高等数学"),new Course("6","大学英语")};
                coursesToSelect.addAll(2,Arrays.asList(courses2));
                Course temp5= (Course) coursesToSelect.get(2);
                Course temp6= (Course) coursesToSelect.get(3);
                //System.out.println("添加了两门课程:"+temp5.id+";"+temp5.name+";"+temp6.id+";"+temp6.name);
    
            }
    
    
            /*
            for each方法访问集合元素  更好
             */
            public void testForEach(){
                System.out.println("有如下课程待选(通过foreach访问:");
                for (Object obj:coursesToSelect){
                    Course cr=(Course)obj;
                    System.out.println("课程:"+cr.id+";"+cr.name);
                }
            }
    
    
        public static void main(String[] args) {
                SetTest st=new SetTest();
                st.testAdd();
                st.testForEach();
                Student student=new Student("1","小埋");
            System.out.println("欢迎学生 "+student.name+"选课");
            //创建一个scanner对象,用来接收从键盘输入的课程ID
            Scanner console=new Scanner(System.in);
    
            for (int i=0;i<3;i++){
                System.out.println("输入课程ID");
                String courseId=console.next();
                for (Course cr:st.coursesToSelect){
                    if (cr.id.equals(courseId)){
                        student.courses.add(cr);
                    }
                }
            }
            st.testForEachForSet(student);
        }
        public void testForEachForSet(Student student){
                //打印输出 学生所选的课程
            System.out.println("xuanle" +student.courses.size()+"ke");
            for (Course cr:student.courses){
                System.out.println("选择了课程 "+cr.id+":"+cr.name);
            }
        }
        }