日期:2014-05-20 浏览次数:20715 次
public static String speak(int number){ final String[] chinese = {"零","一","二","三","四","五","六","七","八","九","十","百"}; if (number <= 10) { return chinese[number] + "号"; } else if (number < 100) { int r = number % 10; int x = number / 10; if (r == 0) { return chinese[x] + chinese[10] + "号"; }else if (x == 1) { return chinese[10] + chinese[r] + "号"; }else { return chinese[x] + chinese[10] + chinese[r] + "号"; } } else { int r = number % 100; int h = number / 100; if (r == 0) { return chinese[h] + chinese[11] + "号"; } else if (r < 10) { return chinese[h] + chinese[11] + chinese[0] + chinese[r] + "号"; } else if (r < 20) { return chinese[h] + chinese[11] + chinese[1] + chinese[10] + chinese[r-10] + "号"; } else { return chinese[h] + chinese[11] + convert(r); } } }
------解决方案--------------------
在huntor的基础上修改了下,最后huntor的递归写错了
public class Changer { public static String speak(int number){ final String[] chinese = {"零","一","二","三","四","五","六","七","八","九","十","百"}; if (number <= 10) { return chinese[number] + "号"; } else if (number < 100) { int r = number % 10; int x = number / 10; if (r == 0) { return chinese[x] + chinese[10] + "号"; }else if (x == 1) { return chinese[10] + chinese[r] + "号"; }else { return chinese[x] + chinese[10] + chinese[r] + "号"; } } else {//三位数的情况 int r = number % 100;// int h = number / 100; //h 表示百位数 if (r == 0) { return chinese[h] + chinese[11] + "号"; } else if (r < 10) { return chinese[h] + chinese[11] + chinese[0] + chinese[r] + "号"; } else if (r < 20) { return chinese[h] + chinese[11] + chinese[1] + chinese[10] + chinese[r-10] + "号"; } else { return chinese[h] + chinese[11] + speak(r); } } } public static void main(String[] args){ for(int i=1;i<999;i++) System.out.println(speak(i)); } }