|
发表于 2015-1-19 21:10:39
|
显示全部楼层
- public class ToBinHex {
- public static void main(String[] args)
- {
- toBinHex(60,2);
- }
- public static void toBinHex(int num, int jinZhi)
- {
- if (jinZhi==2)
- {
- char chs[] = {'0','1'};//两个表可以共用
- char arr[] = new char[32];//两个数组也可以共用
- int weiYi = 1;
- toBinHexTemp(num, jinZhi, chs, arr, weiYi);
- }
- else if(jinZhi==16)
- {
- char chs[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
- char arr[] = new char[8];
- int weiYi = 4;
- toBinHexTemp(num, jinZhi, chs, arr, weiYi);//没定义chs,arr,weiYi这三个变量?为什么?
- }
- else
- {
- System.out.println("暂不支持该进制数");
- return;//记得函数的"break"
- }
- }
- public static void toBinHexTemp(int num, int jinZhi, char chs[], char arr[], int weiYi)
- {
- int pos = 0;
- while(num!=0)
- {
- arr[pos++] = chs[num&(jinZhi-1)];
- num = num>>>weiYi;
- }
- for(int b=pos-1; b>=0; b--)
- {
- if(b!=0)
- System.out.print(arr[b]);
- else
- System.out.println(arr[b]);
- }
- }
- }
复制代码
变量作用域的问题 |
|