一個條件循環(huán)就解決問題。
public static void main(String[] args){
int num = 999;
int count = 0;
do {
? ? ? ? ? ?++count;//起碼是一位,所以要先加一。
? ? ? ? num = num /10; // 位的判斷10的倍數(shù)?
?} while ( num>0? ?); // 滿足數(shù)值在 >0 時重復(fù)執(zhí)行循環(huán)
? ? ? ?System.out.println("它是個"+count+"位的數(shù)");
}
}
2021-04-29
不應(yīng)該是while ( num>1)么,不然會死循環(huán)哎。而且即使是while ( num>1),那num剛好是10的倍數(shù)時,結(jié)果就會不準確,比如num=10,那num/10=1,while結(jié)束循環(huán),此時count=1, 你的結(jié)果會是顯示“它是個1位的數(shù)”
2021-04-02
???? try? ?catch?