/*
需求:在主函數中調用子函數,將10進制數轉化爲2、8、16進制
*/
public class tobin2
{
public static void main(String[] args)
{
bin(253);
eigh(253);
hex(253);
}
/*
十進制——>二進制
*/
static void bin(int num)
{
trans(num,2);
}
/*
十進制——>八進制
*/
static void eigh(int num)
{
trans(num,8);
}
/*
十進制——>16進制
*/
static void hex(int num)
{
trans(num,16);
}
/*
具體計算程序
*/
static void trans(int num,int base)
{
StringBuffer sb =new StringBuffer();
while(num>0)
{
int temp=num%base;
char []arry1= {'0','1','2','3','4','5','6','7','8','9',
'A','B','C','D','E','F'};
sb.append(arry1[temp]);
num/=base;
}
System.out.println(sb.reverse());
}
}