A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© EdwardWuang 中级黑马   /  2016-9-19 09:06  /  3581 人查看  /  28 人回复  /   1 人收藏 转载请遵从CC协议 禁止商业使用本文

第一次点招没过,心里塞塞的,有一道题,写一个工具方法,传入一个int整数,将其转化成中文大写,如123,改写为一百二十三,然后将最后的大写返回,现在还是不会做啊,啊啊啊啊,求教各位大神!!!!!!!!!!!!!!!!!!!!!!!!!

28 个回复

正序浏览
写了个千位以内的,比较简单,再大点就比较繁琐了。

package cn.itcast;

import java.util.Scanner;

/*
* 数字转换汉字小程序,要求如下:
1.键盘录入一个大于-1小于1000的正整数,将该整数转换成中文在控制台输出
2.除非输入数字不在规定的范围,否则可以一直进行输入转换
小于10,数字后不需要带单位
                大于9小于100,数字后需要带单位”十”
                大于99小于1000,数字后需要带单位”百”
                数字转换成中文样式如下:
                5        五
                15       一十五
                115      一百一十五
               
*/
public class Demo2 {
    public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);
               
                String[] arr = {"0","1","2","3","4","5","6","7","8","9"};
                String[] arr1 = {"零","一","二","三","四","五","六","七","八","九"};
                while(true){
                        System.out.println("请输入一个大于-1小于1000的正整数");
                        String s = sc.next();
                        if(!s.matches("\\d+")){
                                System.out.println("不是正整数");
                                return;
                        }
                        for (int i = 0; i < arr1.length; i++) {
                                s=s.replace(arr[i], arr1[i]);
                        }
                        StringBuilder sb = new StringBuilder();
                        sb.append(s);
                        if(sb.length()==1){
                                System.out.println(sb);
                        }else if(sb.length()==2){
                                sb.insert(1, "十");
                                System.out.println(sb);
                        }else if(sb.length()==3){
                                sb.insert(1, "百");
                                sb.insert(3, "十");
                                System.out.println(sb);
                        }else{
                                System.out.println("数字不在规定范围");
                                return;
                        }
                }
        }
}
回复 使用道具 举报
EdwardWuang 发表于 2016-9-19 23:00
看了下大家写的,最多写道1000以内,我这次点招的题目要求写到万位,哭瞎

我写到了亿位的,已经是int 的极限了..就是代码比较啰嗦
回复 使用道具 举报
学到了!!!!
回复 使用道具 举报
感觉这种题如果是有个范围会比较好做
回复 使用道具 举报
看了下大家写的,最多写道1000以内,我这次点招的题目要求写到万位,哭瞎
回复 使用道具 举报
你上什么班的
回复 使用道具 举报
其实不难,可能是你因为没有面试过有点紧张,放松好心态,社招更好过点
回复 使用道具 举报
{:3_50:}这种题碰到了确实就可以等下次了
回复 使用道具 举报
还好,我们做的题是把1000以内的数字变成中文,感觉比这个难
回复 使用道具 举报
刚开始,表述示 并不会      
回复 使用道具 举报
定义String类型的数组
回复 使用道具 举报
点招都这么难吗
回复 使用道具 举报
看了一下,下面还是有大神的
回复 使用道具 举报
这么简单!!!
回复 使用道具 举报
gaol 中级黑马 2016-9-19 22:17:44
14#
学习到了,黑马果然大神多啊
回复 使用道具 举报
厉害,学习到了~
回复 使用道具 举报
import java.util.Scanner;

/**
*  1.        键盘录入一个大于1小于1000的正整数,将该整数转换成中文在控制台输出
*  2.        除非输入数字不在规定的范围,否则可以一直进行输入转换
            小于10,数字后不需要带单位
                大于9小于100,数字后需要带单位”十”
                大于99小于1000,数字后需要带单位”百”
                数字转换成中文样式如下:
                5        五
                15       一十五
                115      一百一十五
*/
public class Prictice7 {
        public static void main(String[] args) {
                String[] s1 = {"零","一","二","三","四","五","六","七","八","九"};
                String[] s2 = {"十","百"};
                Scanner sc = new Scanner(System.in);
               
                while(true) {
                        System.out.println("请录入一个大于1小于1000的正整数:");
                        int num = sc.nextInt();
                        if(num < 1 || num > 1000) {
                                System.out.println("您的输入不合法! 系统结束");
                                System.exit(0);
                        }
                       
                        if(num < 10) {
                                System.out.println(s1[num]);
                        }
                       
                        if(num > 9 && num < 100) {
                                int s = num/10;
                                int g = num%10;
                                System.out.println(s1[s]+s2[0]+s1[g]);
                        }
                       
                        if(num > 99 && num < 1000) {
                                int b = num/100;
                                // 注: 十位,要除以10,再模10
                                int s = num/10%10;
                                int g = num%10;
                                // 判断如果是整百数,直接输入 ..整
                                if(s==0 && g==0) {
                                        System.out.println(s1[b]+s2[1]+"整");
                                } else {
                                        System.out.println(s1[b]+s2[1]+s1[s]+s2[0]+s1[g]);
                                }
                        }
                }
        }
}
回复 使用道具 举报
本帖最后由 431613758 于 2016-9-19 17:42 编辑

import java.util.Scanner;

public class Daxie2 {
        public static void main(String[] args) {
                while (true) {
                        System.out.println("请输入一个大于5小于1000的正整数");
                        int a = new Scanner(System.in).nextInt();
                        if (a < 0 || a > 1000) {
                                System.out.println("超出");
                                continue;
                        }
                        zhuanHuan(a);
        }                               
        }

        public static void zhuanHuan(int a) {
                String[] s={"零","一","二","三","四","五","六","七","八","九"};
                int bai = a / 100;
                int shi = a % 100 / 10;
                int ge = a % 10;
                if (a < 10) {
                        System.out.println(s[ge]);
                }
                if (a >= 10 && a < 100) {
                        if (ge != 0) {
                                System.out.println(s[shi] + "十" + s[ge]);
                        } else {
                                System.out.println(s[shi] + "十");
                        }
                }
                if (a >= 100 && a < 1000) {
                        if (a % 100 / 10 != 0) {
                                if (ge != 0) {
                                        System.out.println(s[bai] + "百" + s[shi] + "十" + s[ge]);
                                } else {
                                        System.out.println(s[bai] + "百" + s[shi] + "十");
                                }
                        }else{
                                if (ge != 0) {
                                        System.out.println(s[bai] + "百" + s[shi] + s[ge]);
                                } else {
                                        System.out.println(s[bai] + "百" );
                                }
                               
                        }
                }
        }
}   }         } }
回复 使用道具 举报
1111111111111111111111111111111
回复 使用道具 举报
我是这么做的:
判断数长度,按需要分成3段,一段亿为单位,一段万为单位,一段没单位
再转成中文配单位,1234>>>一千二百三十四,亿段+"亿",万段+"万"
再处理语法,比方说两个零三个零一起的转成一个零.头和尾都不能为零

大体思路是这样,中间的东西自己去实现,各种办法都行..

本来想来装个B的,没想到写了两个多小时啰啰嗦嗦写了130多行代码,汗...


回复 使用道具 举报
12下一页
您需要登录后才可以回帖 登录 | 加入黑马