java实现数字0-9转换为繁体字

import java.util.Scanner;
public class Switch {
    private static String toChinese(String str) {
        String[] s1 = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
        String[] s2 = { "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千" };
        String result = "";
        int n = str.length();
        for (int i = 0; i < n; i++) {
            int num = str.charAt(i) - '0';
            if (i != n - 1 && num != 0) {
                result += s1[num] + s2[n - 2 - i];
            } else {
                result += s1[num];
            }
        }
        System.out.println(result);
        return result;
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while(true){
            System.out.println("输入字符串:");
            String str = scanner.next();
            // 将字符串数字转化为汉字
            toChinese(str);
        }
    }
}

测试界面如下:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/ligh_sqh/article/details/82970867