关于已知学生名字,修改其ID。希望大神可以指点我一下。。。
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
public class MapTest {
public Map<String, Student> student;
Scanner input = new Scanner(System.in);
public MapTest() {
student = new HashMap<String, Student>();
}
public static void main(String[] args) {
MapTest mapTest=new MapTest();
mapTest.mapAddStu();
mapTest.modifyStu();
}
public void mapAddStu() {
String choice = "t";
String str1,str2;
while (choice.equalsIgnoreCase("t")) {
System.out.println("請輸入要添加學生的學生的ID:");
str1 = input.next();
if (student.get(str1) == null) {
System.out.println("請輸入學生姓名:");
str2 = input.next();
Student stu = new Student(str1, str2);
student.put(str1, stu);
System.out.println("添加成功!\n還要繼續添加嗎?(t/f):");
choice = input.next();
} else {
System.out.println("您輸入的ID已占用,請重新輸入!");
continue;
}
}
System.out.println("添加了一下学生:");
printStu();
}
public void modifyStu(){
String str1,str2,choice="t";
// while(choice.equalsIgnoreCase("t")){
// System.out.println("请输入要修改的学生ID:");
// str1=input.next();
// if(student.containsKey(str1)){
// System.out.println("请输入学生姓名:");
// str2=input.next();
// Student stu1=new Student(str1,str2);
// student.put(str1, stu1);
// System.out.println("还要继续修改吗?");
// choice=input.next();
// }else {
// System.out.println("您输入的ID不存在!");
// continue;
// }
//
// }
choice="t";
int i=0;
while(choice.equalsIgnoreCase("t")){
System.out.println("请输入要修改的学生名字:");
str2=input.next();
Collection<Student> stu=student.values();
for(Student s:stu){
if(str2.equals(s.getName())){
System.out.println("请输入要修改的ID:");
str1=input.next();
//????????????????
i=1;
System.out.println("还要继续修改吗?");
choice=input.next();
break;
}
}
if(i==0){
System.out.println("您输入的学生名字不存在!");
continue;
}
}
printStu();
}
public void printStu(){
Set<Entry<String, Student>> stu=student.entrySet();
for(Entry<String, Student> s:stu){
System.out.println("学生ID:"+s.getKey()+" 学生名字:"+(s.getValue()).getName());
}
}
}问号那里改填什么?
测试





