Java言語プログラム設計とデータ構造」プログラミング練習解答(第21章)(二)


『Java言語プログラム設計とデータ構造』プログラミング練習解答(第21章)(二)
英文名:Introduction to Java Programming and Data Structures,Comprehensive Version,11 th 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("
"
)){ 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[] 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[] 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
忘れた
第二十一章完