想了一会。如果他跟我说int a=023;的话我就直接走了算了。
(023就要判断进制了。。烦 限制10分钟写完)
判断int数字长度的方法。
final static int[] numsize = {9, 99, 999, 9999, 99999, 999999, 9999999, 99999999, 999999999};
public static int sizeOfInt(int x) {
for (int i = 0; ; i++)
if (x <= numsize[i])
return i + 1;
}
既然知道了长度。那接下来肯定是进行求余。当然我们也要判断是否为负数。
public static void main(String[] args) {
int a = -12456;int c =0;int d=0;
if(a>0){
c = sizeOfInt(a);
}else{
d= -a;
c=sizeOfInt(d);
a=d;
System.out.print("-");
}
for (int i = 1; i < c + 1; i++) {
if (c >= 0) {
switch (i) {
case 1:
System.out.print(a % 10);
break;
case 2:
System.out.print(a % 100 / 10);
break;
case 3:
System.out.print(a % 1000 / 100);
break;
case 4:
System.out.print(a % 10000 / 1000);
break;
case 5:
System.out.print(a % 100000 / 10000);
break;
case 6:
System.out.print(a % 1000000 / 100000);
break;
case 7:
System.out.print(a % 10000000 / 1000000);
break;
case 8:
System.out.print(a % 100000000 / 10000000);
break;
case 9:
System.out.print(a % 1000000000 / 100000000);
break;
}
}
}
}
打印的结果
-65421