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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 911288373 中级黑马   /  2012-12-12 18:04  /  1579 人查看  /  8 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

class JiSuanqi
{
        public static void main(String[] args)
                toBin(7);
                toBa(89);
                toHex(60);
}
{
        public static String trans(int num, int base,
                int offset)
        {
                if(num==0)
                {
                        System.out.println(0);
                        return;
                }
                char[] chs = {'0','1','2','3','4','5',
                        '6','7','8','9','A',
                        'B','C','D','E','F'};
                char[] arr = new char[32];
                int pos = arr.length;
                while (num!=0)
                {
                        int temp = num & base;
                        arr[--pos]=chs[temp];
                        num=num>>offset;
                }
                for (int x=pos;x<arr.length ;x++ )
                {
                        System.out.print(arr[x]);
                }
        }
        public static void toBin(int num)
        {
                trans(num,1,1);
        }
        public static void toBa(int num)
        {
                trans(num,7,3);
        }
        public static void toHex(int num)
        {
                trans(num,15,4);
        }
}


主要是返回值的问题, 提示的错误 就是返回值。

评分

参与人数 1技术分 +1 收起 理由
刘芮铭 + 1

查看全部评分

8 个回复

倒序浏览
求解答。   谢谢
回复 使用道具 举报
你不是要返回字符串的类型的返回值嘛~~
现在你返回的是什么啊
回复 使用道具 举报
super_Xiong 发表于 2012-12-12 19:06
你不是要返回字符串的类型的返回值嘛~~
现在你返回的是什么啊

该怎么返啊?
  帮把代码改改?
回复 使用道具 举报
911288373 发表于 2012-12-12 19:33
该怎么返啊?
  帮把代码改改?

class JiSuanqi
{
        public static void main(String[] args)
        {
                toBin(7);
                toBa(89);
                toHex(60);
}

        public static void trans(int num, int base,
                int offset)//这里因为你里面是输出语句,所以不需要返回值,也没有返回接收啊
        {
                if(num==0)
                {
                        System.out.println(0);//不需要返回就不需要return;
                        
                }
                char[] chs = {'0','1','2','3','4','5',
                        '6','7','8','9','A',
                        'B','C','D','E','F'};
                char[] arr = new char[32];
                int pos = arr.length;
                while (num!=0)
                {
                        int temp = num & base;
                        arr[--pos]=chs[temp];
                        num=num>>offset;
                }
                for (int x=pos;x<arr.length ;x++ )
                {
                        System.out.println(arr[x]);
                }
        }
        public static void toBin(int num)
        {
                trans(num,1,1);
        }
        public static void toBa(int num)
        {
                trans(num,7,3);
        }
        public static void toHex(int num)
        {
                trans(num,15,4);
        }
}

评分

参与人数 1黑马币 +2 收起 理由
911288373 + 2 山寨

查看全部评分

回复 使用道具 举报
你的这个代码已经在方法中输出结果了,不需要返回值的,我给你改了一下,你试试,
class  JiSuanqi
{
        public static void main(String[] args)
        {
                        toBin(7);
                toBa(89);
                toHex(60);
        }

        public static void trans(int num, int base, int offset)
        {
                  if(num==0)
              {
                      System.out.println(0);     
              }
                char[] chs = {'0','1','2','3','4','5',
                        '6','7','8','9','A',
                        'B','C','D','E','F'};
                char[] arr = new char[32];
                int pos = arr.length;
                while (num!=0)
                {
                        int temp = num & base;
                        arr[--pos]=chs[temp];
                        num=num>>>offset;
                }
                for (int x=pos;x<arr.length ;x++ )
                {
                        System.out.print(arr[x]);
                        if(x==(arr.length-1))
                        {
                                System.out.println();
                        }
                }
        }
        public static void toBin(int num)
        {
               trans(num,1,1);
        }
        public static void toBa(int num)
        {
                trans(num,7,3);
        }
        public static void toHex(int num)
        {
                trans(num,15,4);
        }
}

评分

参与人数 1黑马币 +22 收起 理由
911288373 + 22 很给力!

查看全部评分

回复 使用道具 举报
焦健 发表于 2012-12-12 20:37
你的这个代码已经在方法中输出结果了,不需要返回值的,我给你改了一下,你试试,
class  JiSuanqi
{

多谢多谢多谢多谢多谢多谢多谢多谢
回复 使用道具 举报
super_Xiong 发表于 2012-12-12 20:15
class JiSuanqi
{
        public static void main(String[] args)

多谢多谢多谢多谢多谢多谢多谢多谢
回复 使用道具 举报
super_Xiong 发表于 2012-12-12 20:15
class JiSuanqi
{
        public static void main(String[] args)

我这个, 前面马虎了, 括号写多了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马