public class HelloWorld
{
public static void main(String[] args)
{
int num=999999;
int count=0;
if(num/10>0)
{
count++;
num=num/10;
if(num>0)
{
if(num<10)
System.out.println("二位数");
}
count++; num=num/10;
if(num>0)
{
if(num<10)
System.out.println("三位数");
}
count++;
num=num/10;
if(num>0)
{
if(num<10)
System.out.println("四位数");
}
count++;
num=num/10;
if(num>0)
{
if(num<10)
System.out.println("五位数");
}
count++; num=num/10;
if(num>0)
{
if(num<10)
System.out.println("六位数");
}
count++;
num=num/10;
if(num>0)
{
if(num<10)
System.out.println("七位数");
}
count++;
num=num/10;
if(num>0)
{
if(num<10)
System.out.println("八位数");
}
count++;
num=num/10;
if(num>0)
{
if(num<10)
System.out.println("九位数");
}
}
else
{
System.out.println("一位数");
}
}
}
为啥不用百分比除呢
int num=999999; String numStr = String.valueOf(num); System.out.println(numStr.length() + "位数");
count先置0,num每除一次10就把count加1,循环条件为num<0,这样当num最后小于0.1的时候,count的值就是几位数了。当然要加上条件num为0单独判断