if(num>=0&&num<=999999999){
while(num!=0){
count++;
num/=10;
}
System.out.println("它是个"+count+"位的数");
}else{
System.out.println("输入有误!");
}
改为for循环
public class HelloWorld{
public static void main(String[] args){
int num = 118579;
int count = 0;
for(count=0;count<10;count++)
{
if(num%10!=0||num>0)
{
num=num/10;
}
else
{
System.out.println("它是个"+count+"位的数!");
break;
}
}
}
}
public class HelloWorld{
public static void main(String[] args){
int num = 999;
int count = 0;
for(;num>0; num=num/10){
if(num%10!=0){
count++;
}
}
System.out.println("它是个"+count+"位的数!");
}
}
public class HelloWorld{
public static void main(String[] args){
int num = 999;
int count = 0;
do
{
count++;
num=num/10;
}
while(num >0);
System.out.println("它是个" + count + "位的数!");
}
}