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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 王晨宇 于 2012-6-14 17:06 编辑

先贴上函数代码
  1. class twelf //函数优化
  2. {
  3. public static void main(String[] args)
  4. {
  5. tobin(6);

  6. }
  7. public static void tobin(int num)
  8. {
  9. trans(num,1,1);

  10. }
  11. public static void tohex(int num)
  12. {
  13. trans(num,15,4);

  14. }
  15. public static void to8int (int num)
  16. {
  17. trans(num,7,3);

  18. }
  19. public static void trans(int num,int base,int fa);
  20. {
  21. if(num==0)
  22. {

  23. System.out.println(0);
  24. return;
  25. }
  26. char[]chs={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
  27. int pos=arr.length;
  28. char[] arr=new char[32];
  29. while (num!=0)
  30. {



  31. int temp=num&base;
  32. arr[--pos]=chs[temp];
  33. num=num>>>fa;
  34. }
  35. for (int x=pos;x<arr.length ;x++ )
  36. {

  37. System.out.print(arr[x]);


  38. }
  39. }

  40. }
复制代码
希望高手给予指点。
我对着看了一个多小时,没看出错在哪儿。给的提示也看了半个小时,愣是没发现问题所在。

  

评分

参与人数 1技术分 +1 收起 理由
黄奕豪 + 1 赞一个!

查看全部评分

3 个回复

正序浏览
  1. class twelf //函数优化
  2. {
  3.                 public static void main(String[] args)
  4.                 {
  5.                         tobin(6);
  6.                 }
  7.                 public static void tobin(int num)
  8.                 {
  9.                         trans(num,1,1);
  10.                 }
  11.                 public static void tohex(int num)
  12.                 {
  13.                         trans(num,15,4);
  14.                 }
  15.                 public static void to8int (int num)
  16.                 {
  17.                         trans(num,7,3);
  18.                 }
  19.                 public static void trans(int num,int base,int fa)//这里没有“;”
  20.                 {
  21.                         if(num==0)
  22.                         {
  23.                                 System.out.println(0);
  24.                                 return;
  25.                         }
  26.                         char[]chs={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
  27.                         int pos=arr.length;//这里应该是int pos=chs.length;
  28.                         char[] arr=new char[32];
  29.                         while (num!=0)
  30.                         {
  31.                                 int temp=num&base;
  32.                                 arr[--pos]=chs[temp];
  33.                                 num=num>>>fa;
  34.                         }
  35.                         for (int x=pos;x<arr.length ;x++ )
  36.                         {
  37.                                 System.out.print(arr[x]);
  38.                         }
  39.                 }
  40. }
复制代码
输出结果110,正确!

评分

参与人数 1技术分 +1 收起 理由
黄奕豪 + 1 赞一个!

查看全部评分

回复 使用道具 举报
我晕了 哥你把这段代码放到MyEclipse上 就会有很多错误了
class Demo //函数优化
{
        public static void main(String[] args)
        {
                tobin(6);

        }
        public static void tobin(int num)
        {
                trans(num,1,1);

        }
        public static void tohex(int num)
        {
                trans(num,15,4);

        }
        public static void to8int (int num)
        {
                trans(num,7,3);

        }
        public static void trans(int num,int base,int fa)//这地方多了一个“;”
        {
                if(num==0)
                {

                        System.out.println(0);
                        return;
                }
                char[] cha={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
                int pos=cha.length;//这个地方你写成了arr               
                                char[] arr=new char[32];
                while (num!=0)
                {



                        int temp=num&base;
                        arr[--pos]=cha[temp];//这个地方你写成了“chs”
                        num=num>>>fa;
                }
                for (int x=pos;x<arr.length ;x++ )
                {

                                System.out.print(arr[x]);


                }
        }

}

还有  不知道 你要的运行结果是什么  只是找出了你写的错误

评分

参与人数 1技术分 +1 收起 理由
黄奕豪 + 1 赞一个!

查看全部评分

回复 使用道具 举报
下面的代码我该了一下,你自己看看就明白了,都注有问题1和问题2

public class tewlf {
public static void main(String[] args) {
  tobin(6);
}
public static void tobin(int num) {
  trans(num, 1, 1);
}
public static void tohex(int num) {
  trans(num, 15, 4);
}
public static void to8int(int num) {
  trans(num, 7, 3);
}
public static void trans(int num, int base, int fa) {//这是个方法,不能用分号,要用括号---------------问题1
  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];//要先声明才能使用,把这里调到上面-------------------问题2
  int pos = arr.length;
  while (num != 0)
  {
   int temp = num & base;
   arr[--pos] = chs[temp];
   num = num >>> fa;
  }
  for (int x = pos; x < arr.length; x++)
  {
   System.out.print(arr[x]);
  }
}
}

评分

参与人数 1技术分 +1 收起 理由
黄奕豪 + 1 赞一个!

查看全部评分

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