int类型的整数转换成汉字
一、源代码:IntegerNumberToChinese.java
1 package cn.com.zfc.example; 2 3 import java.util.Scanner; 4 5 /** 6 * 将一个int类型的整数转换成汉字 7 * 8 * @author zfc 9 *10 */11 public class IntegerNumberToChinese {12 13 // 定义int转换成汉字的方法14 private static String change(int n) { // 数字转换成汉字表示15 String[] c = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };16 String[] d = { "", "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千" };17 String ss = n + "", buffer = new String();18 boolean b = true;19 int j;// 第一次出现零20 for (int i = 0; i < ss.length(); i++) {21 for (j = 0; j <= 9; j++) { // 遍历int整数22 if (ss.charAt(i) == j + '0') {23 break;24 }25 }26 if ((ss.length() + 2) % 4 == 0 && i == 0 && j == 1) {27 buffer += d[ss.length() - i - 1];28 } else if (i != ss.length() - 1 && j == 0) {29 if (b) {30 buffer += c[j];31 b = false;32 }33 } else if (i == ss.length() - 1 && j == 0) {34 if (!b) {35 buffer = buffer.substring(0, buffer.length() - 5);36 } // 如果最后一个也为0,则去掉之前添加的ling37 } else {38 buffer += c[j] + d[ss.length() - i - 1];39 b = true;40 }41 }42 return buffer;43 }44 45 public static void main(String[] args) {46 Scanner sc = new Scanner(System.in);47 System.out.println("请输入int类型的整数:");48 int n = sc.nextInt();49 sc.close();50 System.out.println("对应的汉字大写:" + change(n));51 }52 }
二、运行效果