本文主要是介绍Java语言程序设计与数据结构》编程练习答案(第二十一章)(二),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
《Java语言程序设计与数据结构》编程练习答案(第二十一章)(二)
英文名:Introduction to Java Programming and Data Structures, Comprehensive Version, 11th Edition
21.8
public class book {public static void main(String[] args) {Scanner input = new Scanner(System.in);HashMap<String, Integer> map = new HashMap<>();System.out.println("enter the xxx");String xxx = input.nextLine();String[] words = xxx.split("[\\s,\\,,\\;,\\.,\\:,\\?,\\(,\\),\',\"]");for (String word : words) {String key = word.toLowerCase();if (map.containsKey(key)) {int occur = map.get(key);occur++;map.put(key, occur);} else {map.put(key, 1);}}for(Map.Entry<String, Integer> entry: map.entrySet()){System.out.println(entry.getValue()+"\t"+entry.getKey());}}
}
21.9
public class book {public static void main(String[] args) {Scanner input = new Scanner(System.in);HashMap<String, String> provinces = new HashMap<>();provinces.put("Liaoning","Shenyang");provinces.put("Jilin","Changchun");provinces.put("Heilongjiang","Ha'erbin");System.out.println("Please enter a name of a province:");String pName = input.nextLine();if(provinces.containsKey(pName)){System.out.println("The capital of "+pName+" is "+provinces.get(pName));}else{System.out.println("Please enter a valid province name");}}
}
21.10
public class book {public static void main(String[] args) {Scanner input = new Scanner(System.in);HashMap<String, Integer> keyWords = new HashMap<>();keyWords.put("int",0);keyWords.put("double",0);System.out.println("enter the code");boolean valid = true;while(input.hasNext()){String word = input.next();if(word.equals("//")){valid = false;}else if(word.equals("\n")){valid = true;}else if(word.equals("\"")){valid = !valid;}else{if(keyWords.containsKey(word)){int occur = keyWords.get(word);occur++;keyWords.put(word, occur);}}}}
}
21.11
public class book {public static void main(String[] args) {Scanner input = new Scanner(System.in);ArrayList<HashMap<String, Integer>> boyRank = new ArrayList<>(10);//HashMap<String, Integer>[] girlRank = new HashMap[10];for(int i=0;i<10;i++){boyRank.add(new HashMap<String, Integer>());}boyRank.get(0).put("Tom",1);boyRank.get(0).put("Jerry",2);boyRank.get(1).put("Jerry",1);boyRank.get(1).put("Tom",2);System.out.println("Please enter the year:");int year = input.nextInt();System.out.println("Please enter the name:");String name = input.nextLine();name = input.nextLine();System.out.println("The rank is "+boyRank.get(year).get(name));}
}
21.12
👨有、想🤮了。
21.14
public class book {public static void main(String[] args) {Scanner input = new Scanner(System.in);ArrayList<HashMap<String, Integer>> boyRank = new ArrayList<>(10);//HashMap<String, Integer>[] girlRank = new HashMap[10];for(int i=0;i<10;i++){boyRank.add(new HashMap<String, Integer>());}boyRank.get(0).put("Tom",1);boyRank.get(0).put("Jerry",2);boyRank.get(1).put("Jerry",1);boyRank.get(1).put("Tom",2);while(true) {System.out.println("Please enter the year:");int year = input.nextInt();System.out.println("Please enter the name:");String name = input.nextLine();name = input.nextLine();System.out.println("The rank is " + boyRank.get(year).get(name));System.out.println("Enter another query?");String another = input.nextLine();if(another.equals("N")){break;}}}
}
21.14
爬
21.15
👨忘了
第二十一章 完
这篇关于Java语言程序设计与数据结构》编程练习答案(第二十一章)(二)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!