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

© 安静的喝会奶 中级黑马   /  2016-6-16 17:53  /  404 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

class  SearchExcel
{
        public static void main(String[] args)
        {
                System.out.println("输入您要转换的数:");
                Scanner sc = new Scanner(System.in);
                int num = sc.nexInt();
                System.out.print("转换成8进制为:");
                toOcto(num);
                System.out.println();
                System.out.println("转换成16进制为:");
                toHex(num);
        }
        public static void toOcto(int num)
        {
                excel(num,11,7,3);
        }
        public static void toHex(int num)
        {
                excel(num,8,15,4);
        }
        private static void excel(int num,int len,int and,int shift)
        {
                char[] table = {'0','1','2','3','4',
                                     '5','6','7','8','9',
                                     'A','B','C','D','E','F'};
                if(num==0)
                {
                        System.out.println("转换成"+(and+1)+"进制数是:0");
                        return;
                }
                char[] arr = new char[len];
                int point = arr.length;
                for(int a = num;a!=0; num = num>>shift,a=num)
                {
                        a = a∧
                        arr[--point] = table[a];
                }
                for(int i = point;i<arr.length;i++)
                {
                        System.out.print(arr[i]);
                }
        }
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马