`
baby69yy2000
  • 浏览: 183136 次
  • 性别: Icon_minigender_1
  • 来自: 自己输入城市...
社区版块
存档分类
最新评论

输入1234输出一千二百三十四

    博客分类:
  • Java
阅读更多
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class NumberOutput {

    public static void main(String[] args) throws IOException {
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader bfr = new BufferedReader(isr);
        String input = bfr.readLine();
        int number = 0;
        try {
            number = Integer.parseInt(input);
        } catch (NumberFormatException e) {
            System.out.println("数字格式错误");
            System.exit(1);
        }
        int[] intArr = new int[20];
        String[] strArr = new String[20];
        int i = 0;
        while (number > 0) {
            intArr[i] = number % 10;
            i++;
            number /= 10;
        }

        StringBuffer buf = new StringBuffer();
        for (int j = intArr.length - 1; j >= 0; j--) {

            if (intArr[j] != 0) {
                buf.append(getChinese(intArr[j]));
                buf.append(getPos(j));
            }
        }
        System.out.println(buf);
    }

    public static String getChinese(int i) {
        switch (i) {
        case 0:
            return "零";
        case 1:
            return "一";
        case 2:
            return "二";
        case 3:
            return "三";
        case 4:
            return "四";
        case 5:
            return "五";
        case 6:
            return "六";
        case 7:
            return "七";
        case 8:
            return "八";
        case 9:
            return "九";
        default:
            return "";
        }
    }

    public static String getPos(int i) {
        switch (i) {
        case 0:
            return "";
        case 1:
            return "十";
        case 2:
            return "百";
        case 3:
            return "千";
        case 4:
            return "万";
        case 5:
            return "十万";
        case 6:
            return "百万";
        case 7:
            return "千万";
        case 8:
            return "亿";
        case 9:
            return "十亿";
        default:
            return "";
        }
    }
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics