黑马程序员技术交流社区

标题: 这个题有点错误,谁给找找 [打印本页]

作者: meng    时间: 2013-9-7 17:12
标题: 这个题有点错误,谁给找找
本帖最后由 meng 于 2013-9-9 21:57 编辑

class ArrayTest5
{
        public static void main(String []args)
        {

                toHex(60);
        }

        public static void toHex(int num){//十进制转换成十六进制
               
                StringBuffer sb=new StringBuffer();
                for(int i=0;i<16;i++){
                        int temp=num&15;
                        if(temp>9)
                                sb.append((char)(temp-10+'A'));
                                System.out.println(sb);
                        else
                                sb.append(temp);
                                System.out.println(temp);
                        num=num>>>4;
                        
                }
                System.out.println(sb.reverse());
        }
               
}


作者: 李锡碧    时间: 2013-9-7 17:22
  1. class ArrayTest5 {
  2.         public static void main(String[] args) {

  3.                 toHex(60);
  4.         }

  5.         public static void toHex(int num) {// 十进制转换成十六进制

  6.                 StringBuffer sb = new StringBuffer();
  7.                 for (int i = 0; i < 16; i++) {
  8.                         int temp = num & 15;
  9.                         if (temp > 9) {
  10.                                 sb.append((char) (temp - 10 + 'A'));
  11.                         } else {
  12.                                 sb.append(temp);
  13.                         }
  14.                         num = num >>> 4;

  15.                 }
  16.                 System.out.println(sb.reverse());
  17.         }
  18. }
复制代码

作者: 赵凯    时间: 2013-9-7 18:14
第一种方法:
public static void toHex(int value)    //60  3  12  ----->  3c
        {
                //char y='A';
                //int x=y;
                //System.out.println(x);
                char ch[]= new char[8];
                int index=0;
                while(value!=0)
                {   
                        int num = value&15;   //num=value%16
                        //System.out.println(num);
                        if(num>9)
                        {
                                char c = (char)('A'+(num-10));  //计算出num和10之间的差值,然后加上A的ASCII码,最后进制转化
                                ch[index]=c;
                        }
                        else
                        {
                                ch[index]=(char)('0'+(num-0));
                        }
                        index++;
                        value=value>>>4;     //vaule/16
                }

                for(int i=index-1;i>=0;i--)
                {
                        System.out.print(ch[i]);
                }
        }

第二种方法:
        public static void toHex(int value)    //60  3  12  ----->  3c
        {
                //char y='A';
                //int x=y;
                //System.out.println(x);

                //定义被查找的表
                char chs[]={'0','1','2','3',
                                '4','5','6','7',
                                '8','9','A','B',
                                'C','D', 'E','F'};
                char ch[]= new char[8];
                int index=0;
                while(value!=0)
                {   
                        int num = value&15;   //num=value%16
                    ch[index]=chs[num];
                        index++;
                        value=value>>>4;     //vaule/16
                }

                for(int i=index-1;i>=0;i--)
                {
                        System.out.print(ch[i]);
                }
        }
作者: meng    时间: 2013-9-7 20:24
李锡碧 发表于 2013-9-7 17:22

其实我最主要的困惑是不知道为啥ifelse后面要加{},你能解释一下么

作者: 张文豪    时间: 2013-9-7 21:52
  1. <div class="blockcode"><blockquote>                      if(temp>9)
  2.                                  sb.append((char)(temp-10+'A'));
  3.                                  System.out.println(sb);
  4.                          else
  5.                                  sb.append(temp);
  6.                                  System.out.println(temp);
  7.                          num=num>>>4;
复制代码
if语句有三种格式1.if(){}  2. if(){}else{} 3. if(){}else if(){}...else{}  
在上述代码中 if 下面的语句 当执行完sb.append((char)(temp-10+'A'));后,就已经跳出if的执行语句,if语句结束。它的下一句是不算在if执行语句中。而你下面又写到else,但前面又没有If,这个不符合规定。所以在写代码的时候,要规范书写。。。


作者: 杨增坤    时间: 2013-9-8 07:47
if(条件1){语句A
语句B
}else{          //如果条件1成立,那么会执行语句A和语句 B否则会执行语句C和语句D
语句C
语句D
}


if(条件2){语句A          //若是这样的话,编译会失败的,即使不失败的话,理论上则会执行下满的一条语句
语句C
else         //如果条件1成立,他会执行下面的一条语句,所以它会执行代码A、而不会执行到语句C同理田间不成立,则会下面的一条语句B,而不会执行代
语句B







作者: 杨增坤    时间: 2013-9-8 07:47
楼主你好!


如果您的问题已经解决了,请把您的问题的未解决更改为已解决

谢谢合作!


作者: meng    时间: 2013-9-9 21:54
张文豪 发表于 2013-9-7 21:52
if语句有三种格式1.if(){}  2. if(){}else{} 3. if(){}else if(){}...else{}  
在上述代码中 if 下 ...

好,以后我尽量规范书写





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2