原创

JAVA实现 输入一串数字,转换成汉字显示

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://xxlcube.blog.csdn.net/article/details/8133238
package algo;

import java.util.Scanner;

public class NumTrans {

	private static Scanner sc ;
	
	private static String input;
	
	private static String units[] = {"","十","百","千","万","十","百","千","亿"};
	
	private static String nums[] = {"零","一","二","三","四","五","六","七","八","九","十"};
	
	private static String result[] ;
	
	public static void input() {
		System.out.println("请输入一串数字:如123456,最多9位");
		sc = new Scanner(System.in);
		input = sc.nextLine();
	}
	
	public static String get(String input) {
		String out = "";
		result = new String[input.length()];
		for(int i=0;i<result.length;i++) {
			result[i] = String.valueOf(input.charAt(i));
		}
		int back = 0;
		for(int i=0;i<result.length;i++) {
			if(!result[i].equals("0")) {
				back = result.length-i-1;
				out += nums[Integer.parseInt(result[i])];
				out += units[back];
			}else {
				if(i==result.length-1) {
				
				}else {
					if(!result[i+1].equals("0")) {
						out += nums[0];
					}
				}
			}
		}
		return out;
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		input();
		System.out.println(get(input));
	}

}
请输入一串数字:如123456,最多9位
123456789
一亿二千三百四十五万六千七百八十九
请输入一串数字:如123456,最多9位
100230045
一亿零二十三万零四十五


文章最后发布于: 2012-10-31 16:43:52
展开阅读全文
0 个人打赏
私信求帮助

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 酷酷鲨 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览