如何在我這個代碼中加入判定小于10位
public class HelloWorld{
public static void main(String[] args){
int num = 999;
int count = 0;
do{num=num/10;count++;}while(num>1);
System.out.println("它是個" + count + "位的數!");
}
}
public class HelloWorld{
public static void main(String[] args){
int num = 999;
int count = 0;
do{num=num/10;count++;}while(num>1);
System.out.println("它是個" + count + "位的數!");
}
}
2019-02-15
舉報
2019-02-19
可以直接在while(num>1&&num<999999999)這樣
2019-02-17
我是這樣得到的
public class HelloWorld{
public static void main(String[] args){
int num = 999;
int count = 0;
if (num/1000000000 < 10){
? ? for(num = 999; num/10>=1;count++){
? ? ? ? num = num-9*(num/10);}
? ? ? ? System.out.print("它是個"+count+"位的數!");
}
else{
? ? System.out.print("大于十位");
}
}
}
或者? ? ? if(num > -1000000000 && num < 1000000000) 也ok
2019-02-15
可以在do...while循環外再加個if(num<9999999999),非常直接的方法......