为了账号安全,请及时绑定邮箱和手机立即绑定

为什么我到最后遍历出来还是有三个课程??不应该remove后剩两个吗

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("请输入学生ID:");

int i=0;

while(i<3) {

String console=input.nextLine();

String st=map.get(console);

if(st==null) {

System.out.println("请输入学生姓名:");

String name=input.nextLine();

Student st1=new Student(console,name);

map.put(console, name);

System.out.println("成功添加学生:"+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("添加的学生为:"+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("还有如下学生:"+entry.getKey()+entry.getValue());

}

}

public static void main(String[] args) {

Maptest2 t2=new Maptest2();

t2.test();

t2.test2();

t2.test3();

t2.test4();

}


}


正在回答

3 回答

看你代码test3的第五行,还有第十行

1 回复 有任何疑惑可以回复我~
#1

慕沐4503986 提问者

请大神说一下是那个错误我看不出来
2018-10-12 回复 有任何疑惑可以回复我~
#2

慕前端6229441 回复 慕沐4503986 提问者

remove的参数错了
2018-10-15 回复 有任何疑惑可以回复我~

remove(key)的参数应该是key,而不是学生对象


1 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

为什么我到最后遍历出来还是有三个课程??不应该remove后剩两个吗

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信