做了一个程序是把阿拉伯数字转换为中文数字的,如果输入时夹带有非数字字符也不要紧
编程思想为输入一个字符串,先把前面的0去掉,然后用循环(控制变量为Y)从最后一位开始取数,
如果不为数字,则进入下一轮循环,如果为数字,则位数判断变量I(初始值为-1)+1,I为0时代表个位,
I为1时代表拾位,以此类推。
TEMP2为中文结果(初值为“”),当个位为0时,保留为“”,如果当前数字为0(零)时,如果TEMP2为“”
或以“零”开头,则不加入中文结果TEMP2中(即不会出现“零零”字样)
代码如下:
import java.util.Scanner; //导入输入包
public class Test2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String noInput; // 定义输入变量
String temp1=""; // 定义3个临时变量
String temp2="";
String temp3="";
System.out.print("\n请输入您要转换的阿拉伯数字:(如果出现非阿拉伯数字程序会自动剔除)");
noInput = input.next();
while (!"0".equals(noInput) && noInput.indexOf("0") == 0) { // 循环删除输入数字之前多余的零,如果处理后只为"0"则退出处理
noInput = noInput.substring(1);
}
int i = -1;
for (int y = 0; y < noInput.length(); y++) { // 开始循环取数判断
temp1 = noInput.substring(noInput.length() - y - 1, noInput.length()- y);
if (!("0".equals(temp1) || "1".equals(temp1) || "2".equals(temp1)|| "3".equals(temp1) || "4".equals(temp1)|| "5".equals(temp1) || "6".equals(temp1)|| "7".equals(temp1) || "8".equals(temp1) || "9".equals(temp1))) {
continue;
}
if ("0".equals(temp1)) { // 先由阿拉伯数字转为中文数字,此时不考虑所在的位数
temp1 = "零";
} else if ("1".equals(temp1)) {
temp1 = "壹";
} else if ("2".equals(temp1)) {
temp1 = "贰";
} else if ("3".equals(temp1)) {
temp1 = "叁";
} else if ("4".equals(temp1)) {
temp1 = "肆";
} else if ("5".equals(temp1)) {
temp1 = "伍";
} else if ("6".equals(temp1)) {
temp1 = "陆";
} else if ("7".equals(temp1)) {
temp1 = "柒";
} else if ("8".equals(temp1)) {
temp1 = "捌";
} else if ("9".equals(temp1)) {
temp1 = "玖";
}
i = i + 1;
if (i == 0) { // 个位的判断
if ("零".equals(temp1)) { // 如果个位为零,则中文结果TEMP2保留为”“(空)
continue;
} else { // 如果个位不为零,则中文结果TEMP2更改为对应傎
temp2 = temp1;
continue;
}
} else {
if ("零".equals(temp1)) { // 万位或亿位上的零
if (i % 4 == 0 && i % 8 != 0) { // 如果是万位为零
if ("".equals(temp2) || temp2.indexOf("零") == 0 || temp2.indexOf("亿") == 0) { // 如果中文结果为空或以零字打头又或以亿字打头
temp1 = "万";
} else {
temp1 = "万零";
}
} else if (i % 8 == 0) { // 如果是亿位为零
temp1 = ""; // 临时变量TEMP1初始化
for (int x = 0; x < i / 8; x++) { // 相对应的亿位产生相应多的亿字
temp1 = temp1 + "亿";
}
if (!("".equals(temp2) || temp2.indexOf("零") == 0|| temp2.indexOf("亿") == 0 || temp2.indexOf("万") == 0)) { // 如果中文结果不为空或以零字打头又或以亿字万字打头
temp1 = temp1 + "零";
}
}
} else { // TEMP1不为”0“时,产生出对相应的中文结果
if (i % 4 == 0 && i % 8 != 0) {
temp1 = temp1 + "万";
} else if (i % 8 == 0) {
temp3 = "";
for (int x = 0; x < i / 8; x++) {
temp3 = temp3 + "亿";
}
temp1 = temp1 + temp3;
}
if (i % 4 == 1) {
temp1 = temp1 + "拾";
} else if (i % 4 == 2) {
temp1 = temp1 + "佰";
} else if (i % 4 == 3) {
temp1 = temp1 + "仟";
}
}
}
if ("亿".equals(temp1.substring(temp1.length() - 1, temp1.length())) && temp2.indexOf("万") == 0) { // 如果临时变量TEMP1以亿字结尾且临时变量TEMP2以万字打头
temp2 = temp2.substring(1); // 把TEMP2的打头万字去掉
}
if ("亿".equals(temp1.substring(temp1.length() - 1, temp1.length())) && temp2.indexOf("亿") == 0) { // 如果临时变量TEMP1以亿字结尾且临时变量TEMP2以亿字打头
temp2 = temp2.replaceAll("^亿*(?!亿)", ""); // 用正则表达式消除TEMP2前多余的亿字
}
if ("零".equals(temp1)&& ("".equals(temp2) || temp2.indexOf("零") == 0|| temp2.indexOf("万") == 0 || temp2.indexOf("亿") == 0)) {
continue; // 如果临时变量TEMP1为”零“且临时变量TEMP2为空或以”零“、”万“、”亿“字打头,则不加进TEMP2前
} else if (!"零".equals(temp1)|| ("零".equals(temp1) && temp2.length() > 0)) {
temp2 = temp1 + temp2; // 如果临时变量TEMP1不为“零”,或TEMP1为零而TEMP2上有其它中文结果时,TEMP1加到TEMP2前
}
}
temp2 = temp2.replaceAll("^零*(?!零)", ""); // 用正则表达式消除TEMP2前多余的零字
if ("".equals(temp2)) { // 所有数字处理完后,如果中文结果为空,则输出结果为”零“
temp2 = "零";
}
System.out.println("转换为中文数字为:" + temp2);
}
}欢迎拍砖

解决方案 »

  1.   

    不错,if else多了点儿,看着有点儿乱。
    不知道用正则好不好呢?另外楼上的,没明白用枚举咋弄呢?
      

  2.   

    这么多嵌套就用switch块吧,这么多嵌套,我这里有个不完善的,请大虾帮忙修改!对0的处理不够完整。
    import java.util.Arrays;
    public class Num2Rmb
    {
    private String[] hanArr = {"零" , "壹" , "贰" , "叁" , "肆" , 
    "伍" , "陆" , "柒" , "捌" , "玖"};
    private String[] unitArr = {"十" , "百" , "千"}; /**
     * 把一个浮点数分解成整数部分和小数部分字符串
     * num 需要被分解的浮点数
     * 分解出来的整数部分和小数部分。第一个数组元素是整数部分,第二个数组元素是小数部分。
     */
    private String[] divide(double num)
    {
    //将一个浮点数强制类型转换为long,即得到它的整数部分
    long zheng =  (long)num;
    //浮点数减去整数部分,得到小数部分,小数部分乘以100后再取整得到2位小数
    long xiao = Math.round((num - zheng) * 100);
    //下面用了2种方法把整数转换为字符串
    return new String[]{zheng + "", String.valueOf(xiao)};
    } /**
     * 把一个四位的数字字符串变成汉字字符串
     * numStr 需要被转换的四位的数字字符串
     *  四位的数字字符串被转换成的汉字字符串。
     */
    private String toHanStr(String numStr)
    {
    String result = "";
    int numLen = numStr.length();
    //依次遍历数字字符串的每一位数字
    for (int i = 0 ; i < numLen ; i++ )
    {
    //把char型数字转换成的int型数字,因为它们的ASCII码值恰好相差48
    //因此把char型数字减去48得到int型数字,例如'4'被转换成4。
    int num = numStr.charAt(i) - 48;
    //如果不是最后一位数字,而且数字不是零,则需要添加单位(千、百、十)
    if ( i != numLen - 1 && num != 0)
    {
    result += hanArr[num] + unitArr[numLen - 2 - i];
    }
    //否则不要添加单位
    else
    {
    result += hanArr[num];
    }
    }
    return result;
    }    public static void main(String[] args) 
        {        
    Num2Rmb nr = new Num2Rmb();
    //测试把一个浮点数分解成整数部分和小数部分
    System.out.println(Arrays.toString(nr.divide(1242.156)));
    //测试把一个四位的数字字符串变成汉字字符串
    System.out.println(nr.toHanStr("568"));
        }
    }
      

  3.   

    这么复杂?
    用String的replace()方法不就行了?
      

  4.   

    呵呵,刚学JAVA不到一个月,ACCP 一期的学生,就只能这水平了
      

  5.   

    public class NumRmb {
    private String[] hanArr = {"零","壹","贰","叁","肆","伍","陆","柒","拐","玖"};
    private String[] unitArr = {"十","百","千","万","十","百","千","亿","十","百","千"};

    private String toHanStr(String numStr){
    String result = "";
    int numLen = numStr.length();
    for(int i = 0; i < numLen; i ++){
    int num = numStr.charAt(i) - 48;
    if(i != numLen - 1 && num != 0){
    result += hanArr[num] + unitArr[numLen - 2 - i];
    }
    else
    {
    result +=hanArr[num];
    }
    }
    return result;
    }

    public static void main(String[] args) {
    NumRmb nr = new NumRmb();
    System.out.println("123456789123");
    System.out.println(nr.toHanStr("123456789123"));
    }}
      

  6.   

    package houlei.csdn.lang;import java.util.regex.Matcher;
    import java.util.regex.Pattern;/**
     * 将包含数字的字符串中,数字替换为人民币大写形式
     * 
     * @author 侯磊
     */
    public class NumberTranslator {
    /**
     * 将字符串中数字和字符进行区分
     */
    private static final Pattern NumberPattern = Pattern.compile("([-\\+]?\\d[\\.\\d]*)|(\\D)");
    /**
     * 判断字符串是否是纯数字的字符串
     */
    private static final Pattern IsNumber = Pattern.compile("^[-\\+]?\\d[\\.\\d]*$");
    /**
     * 该接口用于 将 数字转换成人民币大写
     */
    public static interface Translateable {
    char[] NumArray = {'零','壹','贰','叁','肆','伍','陆','柒','拐','玖'};
    char[] UnitArray = {'元','拾','佰','仟','万','拾','佰','仟','亿','拾','佰','仟','万','拾','佰','仟'};
    char[] DecimalUnitArray = {'角','分','厘'};
    /**
     * 传入数字字符串,传出人民币大写的字符串
     */
    String translate(String number);
    }
    /**
     * 公共代码的抽象类
     */
    public static abstract class AbstractTranslateable implements Translateable{
    public String translate(String number) {
    //所有转换的结果都保存在buff对象里面
    StringBuilder buff = new StringBuilder();
    beforeTranslate(buff);
    if(number.startsWith("-")){//如果数字字符串是负数
    appendMinus(buff);
    number = number.substring(1);
    }
    String subNumber [] = number.split("\\.");
    if(subNumber.length==1){//数字字符串是整数(没有小数点)
    appendInteger(buff,subNumber[0]);
    }else{
    appendInteger(buff,subNumber[0]);
    appendPoint(buff);
    appendDecimal(buff,subNumber[1]);
    }
    afterTranslate(buff);
    return buff.toString();
    }
    protected void afterTranslate(StringBuilder buff) {
    // do nothing ...
    }
    protected void beforeTranslate(StringBuilder buff) {
    // do nothing ...
    }
    /**
     * 对负号的处理
     */
    protected void appendMinus(StringBuilder buff){
    buff.append("负"); 
    }
    /**
     * 对小数点的处理
     */
    protected void appendPoint(StringBuilder buff){
    buff.append("点");
    }
    /**
     * 对小数部分的处理
     */
    protected void appendDecimal(StringBuilder buff, String dec) {
    char decArray [] = dec.toCharArray();
    for(int index =0 ; index<decArray.length;index++){
    buff.append(NumArray[decArray[index]-'0']);
    appendDecimalUnit(buff,index);
    }
    }
    /**
     * 对小数单位的处理
     */
    protected void appendDecimalUnit(StringBuilder buff, int index) {
    if(index<DecimalUnitArray.length){
    buff.append(DecimalUnitArray[index]);
    }
    }
    /**
     * 对整数部分的处理
     */
    protected void appendInteger(StringBuilder buff, String integer) {
    char intArray [] = integer.toCharArray();
    for(int index=0;index<intArray.length;index++){
    if(intArray[index]=='0' && intArray[index-1]=='0'){
    if(buff.charAt(buff.length()-1)!=NumArray[0]){
    buff.deleteCharAt(buff.length()-1);//删除由'0'引起的多余单位
    }
    continue;//多个'0'就不进行处理了
    }
    buff.append(NumArray[intArray[index]-'0']);
    buff.append(UnitArray[intArray.length-index-1]);
    }
    if(buff.charAt(buff.length()-2)==NumArray[0]){
    buff.deleteCharAt(buff.length()-2);//删掉最后个位是'0'的转换结果
    }
    }
    }
    /**
     * 默认的转换处理类。通过覆盖抽象类的方法,进行功能的删改。
     */
    private static class DefaultTranslateable extends AbstractTranslateable{
    @Override
    protected void appendDecimalUnit(StringBuilder buff, int index) {
    //do nothing ...
    }
    @Override
    protected void appendInteger(StringBuilder buff, String integer) {
    super.appendInteger(buff, integer);
    buff.deleteCharAt(buff.length()-1);//删除整数部分的'元'单位。
    }
    @Override
    protected void afterTranslate(StringBuilder buff) {
    buff.append(UnitArray[0]);
    }
    }
    private static Translateable DefaultTrans = new DefaultTranslateable();
    private Translateable translateable = DefaultTrans; public String translate(String number) {
    StringBuilder buff = new StringBuilder();
    Matcher m = NumberPattern.matcher(number);
    while (m.find()) {
    String match = number.substring(m.start(), m.end());
    Matcher isNum = IsNumber.matcher(match);
    if (isNum.matches()) {
    buff.append(translateable.translate(match));
    } else {
    buff.append(match);
    }
    }
    return buff.toString();
    } public Translateable getTranslateable() {
    return translateable;
    } public void setTranslateable(Translateable translateable) {
    this.translateable = translateable;
    } /**
     * 最土的测试方法。嗯嗯
     */
    public static void main(String[] args) {
    String test = "我的银行存款不可能是:-120030.45。哈哈,有可能是67,嗯嗯。";
    String result = new NumberTranslator().translate(test);
    System.out.println(result);

    }}