package course;import java.io.*;import java.util.*;import java.util.Map.Entry;public class CreateStudent { private static int num; public Map<String,student> stud;? //Map<鍵,值>[這里的泛型建立了鍵和值的映射關系] a=new HashMap<鍵,值>(); public CreateStudent(int i){ this.stud=new HashMap<String,student>(); //建立hash表 if(confirm(i)){ ? num=i; create();? //如果通過驗證程序則執行創建學生程序 } } private Boolean confirm(int u){? //驗證程序,如果通過返回true,否則返回false try{ if(u<=0||u>=20) throw new IOException(); else? return true; }catch(IOException e){ System.out.println("輸入的數據有誤!"); e.printStackTrace(); return false; } } private void create(){ int i=0; while(i<num){ System.out.println("請輸入學生id"); Scanner scan1=new Scanner(System.in); String str1=scan1.next(); if(stud.get(str1)==null){? //如果此學生id沒有對應的學生對象則繼續下面的步驟 System.out.println("請輸入id="+str1+"的學生姓名"); String nam=scan1.next(); student stu=new student(nam,str1); stud.put(str1, stu); System.out.println("成功添加"+stu.id+stu.name); i++; } else{ System.out.println("該學生id已被占用"); continue; } if(i==num) scan1.close(); } } public void remove(){? Scanner scan2=new Scanner(System.in); while(true){ System.out.println("請輸入要刪除的學生的id"); String str3=scan2.next();//編譯器提示此處有問題 if(stud.get(str3)==null){ System.out.println("此id尚未注冊"); continue; } stud.remove(str3); scan2.close(); break; } }?private void EntrySet(){ //通過entrySet返回Map中所有鍵值對。 Set<Entry<String,student>> Entryset=stud.entrySet();//Entry 為Map的一個內部類 for(Entry<String,student> a:Entryset){ System.out.println("取得鍵"+a.getKey()); System.out.println("值為"+a.getValue().name); } } public static void main(String[] args){ System.out.println("請輸入想要創建的學生個數"); Scanner scan0=new Scanner(System.in); int h=scan0.nextInt(); CreateStudent stu0=new CreateStudent(h); scan0.close(); stu0.output(); stu0.remove(); stu0.EntrySet(); }如題,為什么系統會出現這種提示,這代碼應該如何改進?求大神指教
添加回答
舉報
0/150
提交
取消