Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.HashMap;
- import java.util.Map;
- public class printSorryWithChinese {
- private static Map<Integer, String> map = new HashMap<>();
- static {
- map.put(0, "零");
- map.put(1, "一");
- map.put(2, "二");
- map.put(3, "三");
- map.put(4, "四");
- map.put(5, "五");
- map.put(6, "六");
- map.put(7, "七");
- map.put(8, "八");
- map.put(9, "九");
- map.put(10, "十");
- map.put(100, "百");
- map.put(1000, "千");
- map.put(10000, "万");
- }
- public static String num2Chinese(int n)
- {
- StringBuffer buffer = new StringBuffer();
- boolean addZero = true;
- for (int num = 10000; num >=1; num = num / 10) {
- int mod = n / num;
- if (mod != 0) {
- buffer.append(map.get(mod));
- if (num != 1)
- buffer.append(map.get(num));
- addZero = true;
- }
- else if (buffer.length() != 0 && addZero && num != 1) {
- buffer.append(map.get(0));
- addZero = false;
- }
- n = n % num;
- }
- return buffer.toString();
- }
- public static void main(String[] args)
- {
- System.out.println(num2Chinese(12103));
- }
- }
Add Comment
Please, Sign In to add comment