Guest User

Untitled

a guest
Apr 25th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.Map;
  3.  
  4. public class printSorryWithChinese {
  5.  
  6. private static Map<Integer, String> map = new HashMap<>();
  7.  
  8. static {
  9. map.put(0, "零");
  10. map.put(1, "一");
  11. map.put(2, "二");
  12. map.put(3, "三");
  13. map.put(4, "四");
  14. map.put(5, "五");
  15. map.put(6, "六");
  16. map.put(7, "七");
  17. map.put(8, "八");
  18. map.put(9, "九");
  19. map.put(10, "十");
  20. map.put(100, "百");
  21. map.put(1000, "千");
  22. map.put(10000, "万");
  23. }
  24.  
  25. public static String num2Chinese(int n)
  26. {
  27. StringBuffer buffer = new StringBuffer();
  28. boolean addZero = true;
  29.  
  30. for (int num = 10000; num >=1; num = num / 10) {
  31. int mod = n / num;
  32. if (mod != 0) {
  33. buffer.append(map.get(mod));
  34.  
  35. if (num != 1)
  36. buffer.append(map.get(num));
  37.  
  38. addZero = true;
  39. }
  40. else if (buffer.length() != 0 && addZero && num != 1) {
  41. buffer.append(map.get(0));
  42. addZero = false;
  43. }
  44.  
  45. n = n % num;
  46. }
  47. return buffer.toString();
  48. }
  49.  
  50. public static void main(String[] args)
  51. {
  52. System.out.println(num2Chinese(12103));
  53. }
  54.  
  55. }
Add Comment
Please, Sign In to add comment