求一个数字的长度

方法一:

 static int stringSize1(int x){
        int count = 0;
        while (x > 0) {
            count++;
            x /= 10;
        }
        return count;
    }

方法二:

1 finalstaticint[] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999,
2 99999999, 999999999, Integer.MAX_VALUE };
3 
4 // Requires positive x
5 staticintstringSize(intx) {
6 for(inti=0; ; i++)
7 if(x <= sizeTable[i])
8 returni+1;
9 }

 

posted on 2016-05-27 16:39  wzyxidian  阅读(458)  评论(0编辑  收藏  举报

导航