幫我看下為什么只能打印2門(mén)選擇課程?
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", "離散數(shù)學(xué)");
? ? coureselect.add(c1);
? ?
? ? coure c2=new coure("2", "大學(xué)語(yǔ)文");
? ? coureselect.add(0, c2);
? ?
? ? coure[] c3={new coure("4", "物理學(xué)"),new coure("5", "生物學(xué)")};
? ? coureselect.addAll(Arrays.asList(c3));
? ?
? ? coure[] c4={new coure("6", "四級(jí)英語(yǔ)"),new coure("7", "計(jì)算機(jī)")};
? ? 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("請(qǐng)輸入課程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);
? ? }
? ? }
}
無(wú)論小明選擇幾門(mén)課最后都只打印了兩門(mén)課程
2017-03-05
2017-03-06
你這里面沒(méi)有ID為3 的課程
我也不是大神? 我跟你一樣都是初學(xué)者
2017-03-06
?。靠墒俏耶?dāng)時(shí)運(yùn)行時(shí)有問(wèn)題,那我在運(yùn)行試下,我又有一個(gè)問(wèn)題了,我想做一個(gè)抽獎(jiǎng)的小游戲,結(jié)果卡在第一步了,就是必須注冊(cè)成為會(huì)員才能抽獎(jiǎng),而原本就是會(huì)員的可以直接登錄抽獎(jiǎng),然后登錄這一步用戶很可能輸錯(cuò)嗎,我就想用一個(gè)循環(huán),知道賬號(hào)和密碼全對(duì)才能進(jìn)行下一步,但我不知道循環(huán)該 怎么用了,原本打的代碼被我一氣之下給刪了。大神有什么意見(jiàn)嗎?