日期:2014-05-20  浏览次数:20715 次

java基础题、关于银行叫号程序、求教
我是一个java新手.最近在写一个银行叫号程序:被难住:
求大侠赐教:以下是要求:
银行叫号程序中需要将排队的编号(假设最大为999)转化为对应的汉字,如128转为一百二十八号, 101-一百零一号,10-十号,100-一百号。
 方法原型: String getHao(int id)

------解决方案--------------------
数字不大,暴力解决
Java code
    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的递归写错了
Java code

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));
    }
}