高手解决的问题------------------结帖超快!!!!!!!!

zoehugh 2004-11-19 11:24:48
程序如下所示:
是实现十进制转换为八进制和十进制转换为十六进制。
但是为什么不能实现十进制转换为十六进制呢?
StringBuffer的insert方法哪里用错了。还是别的原因,为什么?
那应该怎么样?
希望高手帮忙解决。
public class Test {
public static void main( String [] args )
{
int a=15;
System.out.println(ba(15));
//System.out.println(shiliu(15));
}

static int shiliu(int shiliu)
{
StringBuffer buf=new StringBuffer( 10 );
while( shiliu != 0 )
{
if( shiliu%16<10)
buf.insert(0,'a');
else if( shiliu%16==10)
buf.insert(0,'b');
else if( shiliu%16==11)
buf.insert(0,'c');
else if( shiliu%16==12)
buf.insert(0,'d');
else if( shiliu%16==13)
buf.insert(0,'e');
else if( shiliu%16==14)
buf.insert(0,'e');
else
buf.insert(0,'f');
shiliu=shiliu/16;
}
return Integer.parseInt(buf.toString());
}

static int ba(int ba )
{
StringBuffer buf=new StringBuffer( 50 );
while( ba != 0 )
{
buf.insert(0,ba%8);
ba=ba/8;
}
return Integer.parseInt(buf.toString());
}
}
...全文
53 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
redex 2004-11-19
  • 打赏
  • 举报
回复
比如要把15转换成一个16进制数的话,你把“f”这个字符串作为parseInt的参数来调用parseInt,而parseInt不能把“f”解析成一个整形数,于是就抛出了一个“NumberFormatException”异常。
classjava 2004-11-19
  • 打赏
  • 举报
回复
??????????????????
晕,倒分早说啦
chanceqw 2004-11-19
  • 打赏
  • 举报
回复
String hexStr=Integer.toHexString(i);
String OctStr=Integer.toOctalString(i);
classjava 2004-11-19
  • 打赏
  • 举报
回复
public class Test
{
public static void main(String[] args)
{
int i=9;
System.out.println(i+"的二进制是"+Integer.toBinaryString(i));
System.out.println(i+"的八进制是"+Integer.toHexString(i));
System.out.println(i+"的十六进制是"+Integer.toOctalString(i));
}
}
redex 2004-11-19
  • 打赏
  • 举报
回复
你要把一个10进制数转换成16进制数,怎么能用“Integer.parseInt(buf.toString());”???

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧