这样写一个循环体就结束了,想不到要用二重循环
public class HelloWorld{
public static void main(String[] args){
int num = 999999999;
int count = 0;
int i;
for (i=1;i<=num;i=i*10){
count++;
}
System.out.println("它是个"+count+"位的数");
}
}
public class HelloWorld{
public static void main(String[] args){
int num = 999999999;
int count = 0;
int i;
for (i=1;i<=num;i=i*10){
count++;
}
System.out.println("它是个"+count+"位的数");
}
}
2021-08-11
没有问题要创造问题,加个给出x位数最大值的功能 😜
public class HelloWorld{ public static void main(String[] args){ int num = 999; int count = 0; int maxLength = 9; // int最大值2147483647,可计算的值最大不能超过9位数 if(maxLength > 9 || num < 1) { System.out.println("请输入小于10位且大于0的数"); } else { int max = 0; int i = 0; while (i < maxLength) { max = (max * 10) + 9; while(num <= max && num != 0 ) { count++; num /= 10; } if (num == 0) { break; } i++; } System.out.println("它是个" + count + "位的数!"); System.out.println(count + "位数最大值为" + max); } } }
举报