本帖最后由 范鹏飞 于 2013-3-30 20:28 编辑
- class ToBin03
- {
- public static void main(String[] args)
- {
- toBin(60);
- System.out.println("Hello World!");
- }
- public static void toBin(int num)
- {
- char[] chs = new char[]{'0','1','2','3'
- ,'4','5','6','7'
- ,'8','9','A','B'
- ,'C','D','E','F'};
- for (int i=0; i<8; i++)
- {
- int temp = num & 15;
- System.out.print(chs[temp]);
- num = num>>>4;
- }
- }
- }
- ------------------------------------------------
- class ToBin01
- {
- public static void main(String[] args)
- {
- toBin(1000);
- System.out.println("Hello World!");
- }
- public static void toBin(int num)
- {
- StringBuffer sb = new StringBuffer();
- while (num>0)
- {
- if (num%16>9)
- {
- sb.append((char)(num%16-10+'A'));
- }else
- {
- sb.append(num%16);
- }
- num/=16;
- }
- System.out.println(sb.reverse());
- }
- }
复制代码 想问下这段程序在执行效率上有什么区别?好像一般都推荐用第一段程序 |