无结果,问题在哪里?
public class Hello1 { public static void main(String[] args) { int num = 999; int count = 0; int a = num/10; while(a > 0 && a < 10){ if(num>=1){ num=num/10; count++; } System.out.println("它是一位"+count+"位数!"); } } }
public class Hello1 { public static void main(String[] args) { int num = 999; int count = 0; int a = num/10; while(a > 0 && a < 10){ if(num>=1){ num=num/10; count++; } System.out.println("它是一位"+count+"位数!"); } } }
2016-07-01
num = 999,然后 int a = num/10,所以a=99,while(a > 0 && a < 10)进不去
if循环if(num>=1)只是一个判断的要这样写用while
所以改正:
public class Hello1 {
public static void main(String[] args) {
int num = 999;
int count = 0;
int a = num/10;
while(a > 0 && a < 100000){//这个范围扩大了,但是变成了true;这里如果用if下面就不需要break
while(num>=1){//if变break
num=num/10;
count++;
}
System.out.println("它是一位"+count+"位数!");
break;//所以有个break
}
}
}
举报