这是晚上练习的一个方法。利用调用,灵活取值,等孩子后学了输入再加进去,看看能不能自己封装一个简易进制转换计算器出来:D
class Turn
{
public static void trans(int num,int base,int offset)
{
if (num == 0)
{
System.out.println(0);
return ;
}
char [] chs = {'0','1','2','3','4','5','6','7','8','9','A','B','C','E','F'};
char [] arr = new char [32];
int pos = arr.length;
while (num != 0)
{
int temp = num & base ;
arr [--pos] = chs [temp] ;
num = num >>> offset ;
}
for (int x = pos ; x < arr.length ; x++ )
{
System.out.print(arr[x]);
}
}
public static void main (String[] args)
{
trans(60,1,1);
System.out.println();
trans(60,7,3);
System.out.println();
trans(60,15,4);
System.out.println();
}
}
|
|