请教我方法2的改进
public class HelloWorld{
public static void main(String[] args){
// 方式1:通过百度搜索到幂运算法加入的
int num = 999;
int count = 0;
for (int i = 0; i <= 10; i++) {
double result = Math.pow(10,i);
if(num - result >= 0) {
count = count +1;
}
}
//方式2:此法有个缺点:没在for之前把count+1,最终位数会少1
/** int num = 999;
int count = 0;
int dig = 1;
if(num > -1000000000 && num < 1000000000){
count = 1;
}
else {
System.out.println("输入的值必须在10位以内");
}
for (int i = 1; i < 10; i++) {
dig = (dig * 10);
if(num - dig >= 0) {
count = count +1;
// System.out.println(dig);
}
}
**/
System.out.println("它是个" + count +"位的数!");
}
}