输入法查找—JAVA

输入法查找—JAVA_第1张图片

import java.util.LinkedHashMap;
import java.util.Scanner;
import java.util.Map.Entry;

public class Main {
    /** * @param args */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        LinkedHashMap hashmap = new LinkedHashMap<String, String>();
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            String b = "";
            String t = in.next();
            if (t.equals("code"))
                continue;
            if (t.contains("_")) {
                String[] str = t.split("_");
                String hanzi = str[0];
                String pinyin = str[1];
                hashmap.put(hanzi, pinyin);
                continue;
            }
            if (t.equals("search"))
                continue;
            int num = 0;
            int count = 0;
            for (int i = 0; i < t.length(); i++)
                if (t.charAt(i) >= '0' && t.charAt(i) <= '9') {
                    num = t.charAt(i) - '0';
                    t = t.substring(0, i);
                    // System.out.println(num);
                }
            for (Object o : hashmap.entrySet()) {
                Entry entry = (Entry) o;
                String key = (String) entry.getKey();
                String value = (String) entry.getValue();
                if (value.equals(t)) {
                    count++;
                    if (count < 6 && num == 0) {
                        b += key + ";";
                        // System.out.print(key+";");
                    } else if (count == num) {
                        System.out.print(key);
                    }
                }
            }
            if (count == 0 || num > count) {
                System.out.print("Error");
            } else if (b.length() >= 1) {
                System.out.println(b.substring(0, b.length() - 1));
            }

        }

    }

}

你可能感兴趣的:(java-输入法查找)