為什么我到最后遍歷出來還是有三個(gè)課程??不應(yīng)該remove后剩兩個(gè)嗎
package pakedate1;
import java.util.*;
import java.util.Map.Entry;
public class Maptest2 {
public Map<String,String>map;
public Maptest2() {
this.map=new HashMap<String,String>();
}public void test() {
Scanner input=new Scanner(System.in);
System.out.println("請輸入學(xué)生ID:");
int i=0;
while(i<3) {
String console=input.nextLine();
String st=map.get(console);
if(st==null) {
System.out.println("請輸入學(xué)生姓名:");
String name=input.nextLine();
Student st1=new Student(console,name);
map.put(console, name);
System.out.println("成功添加學(xué)生:"+map.get(console));
i++;
}else {
System.out.println("已被占用請重新輸入");
continue;
}
}
}public void test2() {
Set<String>st=map.keySet();
for (String string : st) {
String stt=map.get(string);
if(stt!=null) {
System.out.println("添加的學(xué)生為:"+stt);
}
}
}public void test3() {
System.out.println("請輸入待刪除的課程ID");
Scanner input2=new Scanner(System.in);
while(true) {
String number=input2.nextLine();
String stt2=map.get(number);
if(stt2==null) {
System.out.println("不存在該ID");
continue;
}?
map.remove(stt2);
System.out.println("成功刪除課程"+stt2);
break;
}
}public void test4() {
Set<Entry<String, String>>stt3=map.entrySet();
for (Entry<String, String> entry : stt3) {
System.out.println("還有如下學(xué)生:"+entry.getKey()+entry.getValue());
}
}
public static void main(String[] args) {
Maptest2 t2=new Maptest2();
t2.test();
t2.test2();
t2.test3();
t2.test4();
}
}
2018-10-12
看你代碼test3的第五行,還有第十行
2019-09-06
同樣問題的,路過
2018-11-06
remove(key)的參數(shù)應(yīng)該是key,而不是學(xué)生對象