用Java语言将一个键盘输入的数字转化成中文输出

2023-06-29   


将一个键盘输入的数字转化成中文输出
   (例如:输入:1234567 输出:一百二拾三万四千五百六拾七)
   用java语言实现,,请编一段程序实现!
   1. public class Reader
   2. private String strNum;
   3.
   4. private String strNumChFormat;
   5.
   6. private String strNumTemp;
   7.
   8. private int intNumLen;
   9.
   10. private String strBegin;
   11.
   12. public Reader(String strNum)
   13. this.strNum = strNum;
   14.
   15.
   16. public boolean check(String strNum)
   17. boolean valid = false;
   18. if (strNum.substring(0, 1).equals(“0″))
   19. this.strNum = strNum.substring(1);
   20.
   21. try
   22. new Double(strNum);
   23. valid = true;
   24. catch (NumberFormatException ex)
   25. System.out.println(“Bad number format!”);
   26.
   27. return valid;
   28.
   29.
   30. public void init()
   31. strNumChFormat = “”;
   32. intNumLen = strNum.length();
   33. strNumTemp = strNum;
   34. strNumTemp = strNumTemp.replace(’1′, ‘一’);
   35. strNumTemp = strNumTemp.replace(’2′, ‘二’);
   36. strNumTemp = strNumTemp.replace(’3′, ‘三’);
   37. strNumTemp = strNumTemp.replace(’4′, ‘四’);
   38. strNumTemp = strNumTemp.replace(’5′, ‘五’);
   39. strNumTemp = strNumTemp.replace(’6′, ‘六’);
   40. strNumTemp = strNumTemp.replace(’7′, ‘七’);
   41. strNumTemp = strNumTemp.replace(’8′, ‘八’);
   42. strNumTemp = strNumTemp.replace(’9′, ‘九’);
   43. strNumTemp = strNumTemp.replace(’0′, ‘零’);
   44. strNumTemp = strNumTemp.replace(‘.’, ‘点’);
   45. strBegin = strNumTemp.substring(0, 1);
   46.
   47.
   48. public String readNum()
   49. if (check(strNum))
   50. init();
   51. try
   52. for (int i = 1, j = 1, k = 1; i < intNumLen; i++)
   53. if (strNumTemp.charAt(intNumLen – 1) == ‘零’ && i == 1)
   54. strNumChFormat = “位”;
   55. else if (strNumTemp.charAt(intNumLen – i) == ‘零’ && j == 1)
   56. strNumChFormat = “位” + strNumChFormat;
   57. else if (strNumTemp.charAt(intNumLen – i) == ‘点’)
   58. j = 1;
   59. k = 1;
   60. strNumChFormat = strNumTemp.charAt(intNumLen – i) + strNumChFormat;
   61. continue;
   62. else
   63. strNumChFormat = strNumTemp.charAt(intNumLen – i) + strNumChFormat;
   64.
   65. if (strNumTemp.charAt(intNumLen – i – 1) != ‘位’ && strNumTemp.charAt(intNumLen – i – 1) != ‘零’)
   66. if (j == 1 && i < intNumLen)
   67. strNumChFormat = ‘拾’ + strNumChFormat;
   68. else if (j == 2 && i < intNumLen)
   69. strNumChFormat = ‘百’ + strNumChFormat;
   70. else if (j == 3 && i < intNumLen)
   71. strNumChFormat = ‘千’ + strNumChFormat;
   72.
   73.
   74. if (j == 4 && i < intNumLen)
   75. j = 0;
   76.
   77. if (k == 4 && i < intNumLen)
   78. strNumChFormat = ‘万’ + strNumChFormat;
   79. else if (k == 8 && i < intNumLen)
   80. k = 0;
   81. strNumChFormat = ‘亿’ + strNumChFormat;
   82.
   83. j++;
   84. k++;
   85.
   86. while (strNumChFormat.indexOf(“位”) != -1)
   87. strNumChFormat = strNumChFormat.replaceAll(“位”, ” “);
   88.
   89. if (strNumChFormat.substring(0, 2) == “一拾”)
   90. strNumChFormat = strNumChFormat.substring(1, strNumChFormat.length());
   91.
   92. if (strNumChFormat.indexOf(“点”) >= 0)
   93. String rebegin = strNumChFormat.substring(0, strNumChFormat.indexOf(“点”));
   94. String relast = strNumChFormat.substring(strNumChFormat.indexOf(“点”), strNumChFormat.length());
   95. for (int i = 1; i


相关内容:

  1. Java基础知识面试要点
  2. Java编程面试题
  3. 介绍下Java的输入输出流
  4. 请用Python写一个获取用户输入数字,并根据数字大小输出不同信息的脚本
  5. 新东网科技Java笔试题
  6. C语言50道问题