您的位置:首页 > 其它

如何判断一个整数是多少位

2016-08-15 16:11 148 查看

public class TempClass {

 

 

    final static int[] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999,    

            99999999, 999999999, Integer.MAX_VALUE };

    

    static int sizeOfInt(int x) {    

        for (int i = 0;; i++)    

            if (x <= sizeTable[i])    

                return i + 1;    

    }

    

public static void main(String[] args) {

 

String numStr = null;

 

for(int i=1; i<=200 ; i++){

if(sizeOfInt(i) == 1){

numStr = "00" + i;

}

else if(sizeOfInt(i) == 2){

numStr = "0" + i;

}

else{

numStr = "" + i;

}

 

System.out.println("String tag" + numStr + ";");

}

}

 

}

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: