| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 631 人关注过本帖
标题:将字符串的数字转换为大写
只看楼主 加入收藏
卢静
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2015-4-27
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
将字符串的数字转换为大写
package lianxi.biancheng.test;

import java.util.Scanner;

public class T5 {
    public void way1(char m) {
        if (m == '1')
            System.out.print("壹");
        else if (m == '2')
            System.out.print("贰");
        else if (m == '3')
            System.out.print("叁");
        else if (m == '4')
            System.out.print("肆");
        else if (m == '5')
            System.out.print("伍");
        else if (m == '6')
            System.out.print("陆");
        else if (m == '7')
            System.out.print("柒");
        else if (m == '8')
            System.out.print("捌");
        else if (m == '9')
            System.out.print("玖");
    }

    public void way2() {
        System.out.print("零");
    }

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.print("请输入小于等于7的数:");
        String a = s.nextLine();
        String b = a.trim();
        char[] ch = null;
        ch = b.toCharArray();
        T5 p = new T5();
        if (ch.length == 1) {
            if (ch[0] != '0')
                p.way1(ch[0]);
            else
                p.way2();
        }
        if (ch.length == 2) {
            if (ch[0] != '0') {
                p.way1(ch[0]);
                System.out.print("拾");
                if (ch[1] != '0')
                    p.way1(ch[1]);
            }
        }
        if (ch.length == 3) {
            if (ch[0] != '0') {
                p.way1(ch[0]);
                System.out.print("佰");
                if (ch[1] != '0') {
                    p.way1(ch[1]);
                    System.out.print("拾");
                    if (ch[2] != '0') {
                        p.way1(ch[2]);
                    }
                } else {
                    if (ch[2] != '0') {
                        p.way2();
                        p.way1(ch[2]);
                    }
                }
            }
        }
        if (ch.length == 4) {
            if (ch[0] != '0') {
                p.way1(ch[0]);
                System.out.print("千");
                if (ch[1] != '0') {
                    p.way1(ch[1]);
                    System.out.print("佰");
                    if (ch[2] != '0') {
                        p.way1(ch[2]);
                        System.out.print("拾");
                        if (ch[3] != '0') {
                            p.way1(ch[3]);
                        }
                    } else {
                        if (ch[3] != '0') {
                            p.way2();
                            p.way1(ch[3]);
                        }
                    }
                } else {
                    if (ch[2] != '0') {
                        p.way2();
                        p.way1(ch[2]);
                        System.out.print("拾");
                        if (ch[3] != '0') {
                            p.way1(ch[3]);
                        }
                    } else {
                        if (ch[3] != '0') {
                            p.way2();
                            p.way1(ch[3]);

                        }
                    }
                }
            }
        }
        if (ch.length == 5) {
            if (ch[0] != '0') {
                p.way1(ch[0]);
                System.out.print("万");
                if (ch[1] != '0') {
                    p.way1(ch[1]);
                    System.out.print("千");
                    if (ch[2] != '0') {
                        p.way1(ch[2]);
                        System.out.print("佰");
                        if (ch[3] != '0') {
                            p.way1(ch[3]);
                            System.out.print("拾");
                            if (ch[4] != 0)
                                p.way1(ch[4]);
                        } else {
                            if (ch[4] != '0')
                                p.way2();
                            p.way1(ch[4]);
                        }
                    } else {
                        if (ch[3] != '0') {
                            p.way2();
                            p.way1(ch[3]);
                            System.out.print("拾");
                            if (ch[4] != '0')
                                p.way1(ch[4]);
                        }
                    }
                } else {
                    if (ch[2] != '0') {
                        p.way2();
                        p.way1(ch[2]);
                        System.out.print("佰");
                        if (ch[3] != '0') {
                            p.way1(ch[3]);
                            System.out.print("拾");
                            if (ch[4] != '0') {
                                p.way1(ch[4]);
                            }
                        } else {
                            if (ch[4] != '0')
                                p.way2();
                            p.way1(ch[4]);
                        }

                    } else {
                        if (ch[3] != '0') {
                            p.way2();
                            p.way1(ch[3]);
                            System.out.print("拾");
                            if (ch[4] != '0')
                                p.way1(ch[4]);
                        } else {
                            if (ch[4] != '0')
                                p.way2();
                            p.way1(ch[4]);
                        }
                    }
                }
            }
        }
        if (ch.length == 6) {
            if (ch[0] != '0') {
                p.way1(ch[0]);
                System.out.print("拾");
                if (ch[1] != '0') {
                    p.way1(ch[1]);
                    System.out.print("万");
                    if (ch[2] != '0') {
                        p.way1(ch[2]);
                        System.out.print("千");
                        if (ch[3] != '0') {
                            p.way1(ch[3]);
                            System.out.print("佰");
                            if (ch[4] != '0') {
                                p.way1(ch[4]);
                                System.out.print("拾");
                                if (ch[5] != '0')
                                    p.way1(ch[5]);
                            } else {
                                if (ch[5] != '0') {
                                    p.way2();
                                    p.way1(ch[5]);
                                }
                            }
                        } else {
                            if (ch[4] != '0') {
                                p.way2();
                                p.way1(ch[4]);
                                System.out.print("拾");
                                if (ch[5] != '0')
                                    p.way1(ch[5]);
                            } else {
                                if (ch[5] != '0') {
                                    p.way2();
                                    p.way1(ch[5]);
                                }
                            }
                        }
                    } else {// ,万位不等于0,千位上等于0
                        if (ch[3] != '0') {
                            p.way2();
                            p.way1(ch[3]);
                            System.out.print("佰");
                            if (ch[4] != '0') {
                                p.way1(ch[4]);
                                System.out.print("拾");
                                if (ch[5] != '0') {
                                    p.way1(ch[5]);
                                }
                            } else {
                                if (ch[5] != '0') {
                                    p.way2();
                                    p.way1(ch[5]);
                                }
                            }
                        } else {
                            if (ch[4] != '0') {
                                p.way2();
                                p.way1(ch[4]);
                                System.out.print("拾");
                                if (ch[5] != '0') {
                                    p.way1(ch[5]);
                                }
                            } else {
                                if (ch[5] != '0') {
                                    p.way2();
                                    p.way1(ch[5]);
                                }
                            }
                        }
                    }
                } else {// 万位上为0
                    System.out.print("万");
                    if (ch[2] != '0') {
                        p.way1(ch[2]);
                        System.out.print("千");
                    } else {// 万位千位都为0
                        if (ch[3] != '0') {
                            p.way2();
                            p.way1(ch[3]);
                            System.out.print("佰");
                            if (ch[4] != '0') {
                                p.way1(ch[4]);
                                System.out.print("拾");
                                if (ch[5] != '0') {
                                    p.way1(ch[5]);
                                }
                            } else {
                                if (ch[5] != '0') {
                                    p.way2();
                                    p.way1(ch[5]);
                                }
                            }
                        } else {// 万千百位都为0
                            if (ch[4] != '0') {
                                p.way2();
                                p.way1(ch[4]);
                                System.out.print("拾");
                                if (ch[5] != '0') {
                                    p.way1(ch[5]);
                                }
                            } else {
                                if (ch[5] != '0') {
                                    p.way2();
                                    p.way1(ch[5]);
                                }
                            }
                        }
                    }
                }
            }
        }
        if (ch.length == 7) {
            if (ch[0] != '0') {
                p.way1(ch[0]);
                System.out.print("佰");
                if (ch[1] != '0') {
                    p.way1(ch[1]);
                    System.out.print("拾");
                    if (ch[2] != '0') {
                        p.way1(ch[2]);
                        System.out.print("万");
                        if (ch[3] != '0') {
                            p.way1(ch[3]);
                            System.out.print("千");
                            if (ch[4] != '0') {
                                p.way1(ch[4]);
                                System.out.print("佰");
                                if (ch[5] != '0') {
                                    p.way1(ch[5]);
                                    System.out.print("拾");
                                    if (ch[6] != '0')
                                        p.way1(ch[6]);
                                } else {
                                    if (ch[6] != '0') {
                                        p.way2();
                                        p.way1(ch[6]);
                                    }
                                }
                            } else {
                                if (ch[5] != '0') {
                                    p.way2();
                                    p.way1(ch[5]);
                                    System.out.print("拾");
                                    if (ch[6] != '0')
                                        p.way1(ch[6]);
                                } else {
                                    if (ch[6] != '0') {
                                        p.way2();
                                        p.way1(ch[6]);
                                    }
                                }
                            }
                        }
                    } else {
                        System.out.print("万");
                        if (ch[3] != '0') {
                            p.way1(ch[3]);
                            System.out.print("千");
                            if (ch[4] != '0') {
                                p.way1(ch[4]);
                                System.out.print("佰");
                                if (ch[5] != '0') {
                                    p.way1(ch[5]);
                                    System.out.print("拾");
                                    if (ch[6] != '0')
                                        p.way1(ch[6]);
                                } else {
                                    if (ch[6] != '0') {
                                        p.way2();
                                        p.way1(ch[6]);
                                    }
                                }
                            } else {
                                if (ch[5] != '0') {
                                    p.way2();
                                    p.way1(ch[5]);
                                    System.out.print("拾");
                                    if (ch[6] != '0')
                                        p.way1(ch[6]);
                                } else {
                                    if (ch[6] != '0') {
                                        p.way2();
                                        p.way1(ch[6]);
                                    }
                                }
                            }
                        }
                    }
                } else {
                    if (ch[2] != '0') {
                        p.way2();
                        p.way1(ch[2]);
                        System.out.print("万");
                        if (ch[3] != '0') {
                            p.way1(ch[3]);
                            System.out.print("千");
                            if (ch[4] != '0') {
                                p.way1(ch[4]);
                                System.out.print("佰");
                                if (ch[5] != '0') {
                                    p.way1(ch[5]);
                                    System.out.print("拾");
                                    if (ch[6] != '0')
                                        p.way1(ch[6]);
                                } else {
                                    if (ch[6] != '0') {
                                        p.way2();
                                        p.way1(ch[6]);
                                    }
                                }
                            } else {
                                if (ch[5] != '0') {
                                    p.way2();
                                    p.way1(ch[5]);
                                    System.out.print("拾");
                                    if (ch[6] != '0')
                                        p.way1(ch[6]);
                                } else {
                                    if (ch[6] != '0') {
                                        p.way2();
                                        p.way1(ch[6]);
                                    }
                                }
                            }
                        }

                    } else {
                        if (ch[3] != '0') {
                            System.out.print("万");
                            if (ch[3] != '0') {
                                p.way1(ch[3]);
                                System.out.print("千");
                                if (ch[4] != '0') {
                                    p.way1(ch[4]);
                                    System.out.print("佰");
                                    if (ch[5] != '0') {
                                        p.way1(ch[5]);
                                        System.out.print("拾");
                                        if (ch[6] != '0')
                                            p.way1(ch[6]);
                                    } else {
                                        if (ch[6] != '0') {
                                            p.way2();
                                            p.way1(ch[6]);
                                        }
                                    }
                                } else {
                                    if (ch[5] != '0') {
                                        p.way2();
                                        p.way1(ch[5]);
                                        System.out.print("拾");
                                        if (ch[6] != '0')
                                            p.way1(ch[6]);
                                    } else {
                                        if (ch[6] != '0') {
                                            p.way2();
                                            p.way1(ch[6]);
                                        }
                                    }
                                }
                            }
                        } else {
                            if (ch[4] != '0') {
                                System.out.print("万");
                                p.way2();
                                p.way1(ch[4]);
                                System.out.print("佰");
                                if (ch[5] != '0') {
                                    p.way1(ch[5]);
                                    System.out.print("拾");
                                    if (ch[6] != '0')
                                        p.way1(ch[6]);
                                } else {
                                    if (ch[6] != '0') {
                                        p.way2();
                                        p.way1(ch[6]);
                                    }
                                }
                            } else {
                                if (ch[5] != '0') {
                                    System.out.print("万");
                                    p.way2();
                                    p.way1(ch[5]);
                                    System.out.print("拾");
                                    if (ch[6] != '0')
                                        p.way1(ch[6]);
                                } else {
                                    if (ch[6] != '0') {
                                        System.out.print("万");
                                        p.way2();
                                        p.way1(ch[6]);
                                    } else
                                        System.out.print("万");
                                }
                            }
                        }
                    }
                }
            }

        }

    }
}
搜索更多相关主题的帖子: package public import 字符串 
2015-04-27 14:58
hhwz
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:87
帖 子:687
专家分:4502
注 册:2011-5-22
收藏
得分:10 
你这方法 太。。。。。。
应该把每个共用的提取出来 创建一个方法
2015-04-27 15:32
凌风zx
Rank: 8Rank: 8
来 自:万灵娱乐城
等 级:蝙蝠侠
威 望:7
帖 子:88
专家分:790
注 册:2015-4-11
收藏
得分:10 
做的不错
我觉得那些大写数和单位用数组存一下会更方便
程序=数据结构+算法
好的算法需要好的数据结构去支持

初出茅庐
2015-05-04 09:47
快速回复:将字符串的数字转换为大写
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.024359 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved