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<鍵,值>[這里的泛型建立了鍵和值的映射關(guān)系] a=new HashMap<鍵,值>(); public CreateStudent(int i){ this.stud=new HashMap<String,student>(); //建立hash表 if(confirm(i)){ ? num=i; create();? //如果通過(guò)驗(yàn)證程序則執(zhí)行創(chuàng)建學(xué)生程序 } } private Boolean confirm(int u){? //驗(yàn)證程序,如果通過(guò)返回true,否則返回false try{ if(u<=0||u>=20) throw new IOException(); else? return true; }catch(IOException e){ System.out.println("輸入的數(shù)據(jù)有誤!"); e.printStackTrace(); return false; } } private void create(){ int i=0; while(i<num){ System.out.println("請(qǐng)輸入學(xué)生id"); Scanner scan1=new Scanner(System.in); String str1=scan1.next(); if(stud.get(str1)==null){? //如果此學(xué)生id沒(méi)有對(duì)應(yīng)的學(xué)生對(duì)象則繼續(xù)下面的步驟 System.out.println("請(qǐng)輸入id="+str1+"的學(xué)生姓名"); 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("該學(xué)生id已被占用"); continue; } if(i==num) scan1.close(); } } public void remove(){? Scanner scan2=new Scanner(System.in); while(true){ System.out.println("請(qǐng)輸入要?jiǎng)h除的學(xué)生的id"); String str3=scan2.next();//編譯器提示此處有問(wèn)題 if(stud.get(str3)==null){ System.out.println("此id尚未注冊(cè)"); continue; } stud.remove(str3); scan2.close(); break; } }?private void EntrySet(){ //通過(guò)entrySet返回Map中所有鍵值對(duì)。 Set<Entry<String,student>> Entryset=stud.entrySet();//Entry 為Map的一個(gè)內(nèi)部類(lèi) 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("請(qǐng)輸入想要?jiǎng)?chuàng)建的學(xué)生個(gè)數(shù)"); Scanner scan0=new Scanner(System.in); int h=scan0.nextInt(); CreateStudent stu0=new CreateStudent(h); scan0.close(); stu0.output(); stu0.remove(); stu0.EntrySet(); }如題,為什么系統(tǒng)會(huì)出現(xiàn)這種提示,這代碼應(yīng)該如何改進(jìn)?求大神指教
什么情況下編譯器會(huì)提示” java.util.NoSuchElementException“
未來(lái)開(kāi)拓者
2016-04-30 18:22:32