继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Java程序:输入数字转换成中文输出

至繁归于至简_
关注TA
已关注
手记 6
粉丝 7
获赞 27

功能看似简单,但真正实现起来也不是易事
现在贴出代码,欢迎各位幕友提出建议!!

package test01;

import java.util.Scanner;

public class Test {

    public static void main(String[] args) {
        System.out.println("欢迎使用数字转换器(Designed by chph)!!");
        String in;
        boolean isRunning = true;
        Scanner scanner = new Scanner(System.in);
        while (isRunning) {
            System.out.println("请输入一串纯整数:");
            in = scanner.next();
            String input = in.trim();
            if (!isNum(input)) {
                System.out.println("输入类型错误!!!你应该输入12位以内纯正整数!!!");
                continue;
            } else {
                System.out.println("转换成中文数字为:" + num2Ch(input));
            }
            System.out.println("是否继续查询?  1.是  2.不,谢谢");
            int isContinue = scanner.nextInt();
            if (isContinue == 1) {
                continue;
            } else if (isContinue == 2) {
                System.out.println("程序结束!欢迎下次使用!!!");
                isRunning = !isRunning;
            } else {
                System.out.println("指令输入错误,直接退出!!");
                System.out.println("程序结束!欢迎下次使用!!!");
                isRunning = !isRunning;
            }
        }
        System.out.println("请输入任意数字退出控制台...");
        in = scanner.next();
        scanner.close();
    }

    private static String num2Ch(String input) {
        input = Long.parseLong(input) + "";
        if (input.equals("0")) {
            return "零";
        }
        // int length = input.length();
        // char[] arr1 = { 'O', '一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '百',
        // '千', '万', '亿' };
        // char[] arr2 = { '零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖', '拾', '佰',
        // '仟', '万', '亿' };
        StringBuffer s = new StringBuffer();
        switch (4 - (input.length() % 4)) {
        case 1:
            s = new StringBuffer("0").append(input);
            break;
        case 2:
            s = new StringBuffer("00").append(input);
            break;
        case 3:
            s = new StringBuffer("000").append(input);
            break;
        default:
            s = new StringBuffer("").append(input);
        }
        String[] arr = new String[s.length() / 4];
        for (int i = 0; i < s.length(); i = i + 4) {
            if (i + 4 == s.length())
                arr[i / 4] = s.toString().substring(i);
            else
                arr[i / 4] = s.toString().substring(i, i + 4);
        }
        StringBuffer output = new StringBuffer("");
        switch (s.length() / 4) {
        case 3:
            output = output.append(trans(arr[s.length() / 4 - 3])).append("亿");
        case 2:
            if (Integer.parseInt(arr[s.length() / 4 - 2]) == 0 && Integer.parseInt(arr[s.length() / 4 - 3]) != 0)
                output = output.append("零");
            else
                output = output.append(trans(arr[s.length() / 4 - 2])).append("万");
        case 1:
            output = output.append(trans(arr[s.length() / 4 - 1]));
            break;
        }
        return output.toString();
    }

    // 四位数转换函数
    private static String trans(String string) {
        String s = Integer.parseInt(string) + "";
        StringBuffer output = new StringBuffer("");
        String[] arr = { "", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
        // for (int i = 0; i < s.length(); i++) {
        // if (s.charAt(i) != 0) {
        // s = s.substring(i);
        // break;
        // }
        // }
        if ((Integer.parseInt(s) + "").equals("")) {
            return "";
        } else {
            switch (s.length()) {
            case 4:
                if (Integer.parseInt(string.charAt(0) + "") == 0) {
                    output = output.append("零");
                }else {
                    output = output.append(arr[Integer.parseInt(s.charAt(0) + "")]).append("千");
                }
            case 3:
                if (Integer.parseInt(string.charAt(3) + "") != 0 && Integer.parseInt(string.charAt(1) + "") == 0 && Integer.parseInt(string.charAt(0) + "") != 0) {
                    output = output.append("零");
                }else {
                    output = output.append(arr[Integer.parseInt(string.charAt(1) + "")]);
                }
                if (Integer.parseInt(string.charAt(1) + "") != 0) {
                    output = output.append("百");
                }
            case 2:
                if (Integer.parseInt(string.charAt(3) + "") != 0 && Integer.parseInt(string.charAt(2) + "") == 0 && Integer.parseInt(string.charAt(1) + "") != 0
                        && Integer.parseInt(string.charAt(0) + "") != 0) {
                    output = output.append("零");
                }else{
                    output = output.append(arr[Integer.parseInt(string.charAt(2) + "")]);
                }
                if (Integer.parseInt(string.charAt(2) + "") != 0) {
                    output = output.append("十");
                }
            case 1:
                if (Integer.parseInt(string.charAt(3) + "") != 0) {
                    output = output.append(arr[Integer.parseInt(string.charAt(3) + "")]);
                }
                break;
            }
            return output.toString();
        }
    }

    // 判断是否是纯数字输入
    private static boolean isNum(String s) {
        String num = "0123456789";
        int length = 0;
        for (int i = 0; i < s.length(); i++) {
            for (int j = 0; j < num.length(); j++) {
                if (s.charAt(i) == num.charAt(j)) {
                    length++;
                    break;
                }
            }
        }
        return (s.trim().length() != 0 && length == s.length() && length <= 12 && length >= 0) ? true : false;
    }

}
打开App,阅读手记
2人推荐
发表评论
随时随地看视频慕课网APP