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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© HEIMAZGP 中级黑马   /  2016-6-4 23:19  /  672 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

class Demo8
{
        public static void main(String[] args)
       
        {
                toHex(60);

        }

                public static void toHex(int num)
               
                {

                        char[] chs = new char[]{'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};

                        char[] arr = new char[8];

                        int pos = 0;
                       
                        while(num!=0)
                        {

                                int temp = num & 15;

                                arr[pos++] = chs[temp];
       
                                num = num >>> 4;

                        }
       
                        System.out.println("pos="+pos);
                                       
                                               

                        for(int x=0 ;x<arr.length; x++)       
       
                        {

                                System.out.print(arr[x]+",");
       
                        }

       
       
                }
}                                               
以上代码在我电脑运行异常,放到同学那儿电脑就没问题,求大神帮助
本人电脑运行结果:pos=2     C,3,a,a,a,a,a,a,
同学电脑运行结果:pos=2     C,3,  ,  ,  ,  ,  , ,

2 个回复

倒序浏览
自己顶下
回复 使用道具 举报
你定义的数组里面没有a  不清楚是怎么回事   但是我重新优化了一下  你可以参考  能不能解决问题import java.util.ArrayList;   class Demo8 {         public static void main(String[] args)                  {                 toHex(60);          }                  public static void toHex(int num)                                  {                          char[] chs = new char[]{'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};                         ArrayList<Object> al = new ArrayList<>();                          int pos = 0;                                                  while(num!=0)                         {                                  int temp = num & 15;                                                                  al.add(chs[temp]) ;                                          num = num >>> 4;                          }                                  System.out.println("pos="+pos);                         StringBuffer stringBuffer = new StringBuffer();                         for(int x=0 ;x<al.size(); x++){                                 stringBuffer.append(al.get(x));                         }                         System.out.println(stringBuffer);                                                             } }   
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马