问答详情
源自:4-11 学生选课---通过 Set 集合管理课程

帮我看下为什么只能打印2门选择课程?

package aaa;


import java.util.HashSet;

import java.util.Set;


public class student {

        public String id;

        public String name;

        public   Set<coure> coures;

        

        public student(){}

        public student(String id,String name)

        {

        this.id=id;

        this.name=name;

        this.coures=new HashSet<coure>();

        }

        

}

package aaa;


public class coure {

public String id;

     public String name;

     public coure(){}

     public coure(String id,String name)

     {

    this.id=id;

    this.name=name;

     

     }

}

package aaa;


import java.util.ArrayList;

import java.util.Arrays;

import java.util.List;

import java.util.Scanner;


public class testset {

    public List<coure> coureselect;

    public testset()

    {

    this.coureselect=new ArrayList<coure>();

    }

    

    public void testadd()

    {

    coure c1=new coure("1", "离散数学");

    coureselect.add(c1);

   

    coure c2=new coure("2", "大学语文");

    coureselect.add(0, c2);

   

    coure[] c3={new coure("4", "物理学"),new coure("5", "生物学")};

    coureselect.addAll(Arrays.asList(c3));

   

    coure[] c4={new coure("6", "四级英语"),new coure("7", "计算机")};

    coureselect.addAll(0, Arrays.asList(c4));

    }

   

    

    public void testforeach()

    {

    for(coure cc:coureselect)

    {

    System.out.println("课程查询:"+cc.id+"  "+cc.name);

    }

    }

    

    

    

    public static void main(String[] args) {

testset t=new testset();

t.testadd();

t.testforeach();

student s=new student("1","小明");

System.out.println("欢迎"+s.name+"选课");

Scanner input=new Scanner(System.in);

for(int i=0;i<4;i++)

{

System.out.println("请输入课程ID:");

String coureid=input.next();

for(coure cr:t.coureselect)

{

if(cr.id.equals(coureid))

{

s.coures.add(cr);

}

}

}

t.foreachset(s);

}

    public void foreachset(student s)

    {

    for(coure cr:s.coures)

    {

    System.out.println("选择课程:"+cr.id+" "+cr.name);

    }

    }

}

无论小明选择几门课最后都只打印了两门课程

提问者:辜谷谷 2017-03-05 10:23

个回答

  • dawei_123
    2017-03-05 15:22:45
    已采纳

    http://img.mukewang.com/58bbbca600011f3902410507.jpg把你的代码运行了一下  没有问题啊???

  • dawei_123
    2017-03-06 20:34:43

    你这里面没有ID为3 的课程

    我也不是大神  我跟你一样都是初学者

  • 辜谷谷
    2017-03-06 15:07:29

    啊?可是我当时运行时有问题,那我在运行试下,我又有一个问题了,我想做一个抽奖的小游戏,结果卡在第一步了,就是必须注册成为会员才能抽奖,而原本就是会员的可以直接登录抽奖,然后登录这一步用户很可能输错吗,我就想用一个循环,知道账号和密码全对才能进行下一步,但我不知道循环该 怎么用了,原本打的代码被我一气之下给删了。大神有什么意见吗?