3 回答

TA貢獻(xiàn)7條經(jīng)驗(yàn) 獲得超7個(gè)贊
int n=2333;
int count=0;
while(n>0){
count++;
n=n/10;
}
System.out.println("該數(shù)是"+count+"位");

TA貢獻(xiàn)3條經(jīng)驗(yàn) 獲得超2個(gè)贊
提供另一種方法,即用 do...while循環(huán)語句編寫:
package com.h3c.test;
public class cq1 {
??? public cq1() {
??? }
??? public static void main(String[] args) {
??????? cq1 c =new cq1();
??????? System.out.println("該數(shù)是"+c.getDigit(100000)+"位");
??? }
??? /**
???? * 判斷一個(gè)數(shù)的位數(shù)
???? * @param int 需要判斷的數(shù)
???? * @return int 位數(shù)
???? */
??? public int getDigit(int? n){
??????? int count =0;
??????? do {
??????????? count++;
??????????? n/=10;
??????? }while(n>0);
??????? return count;
??? }
}

TA貢獻(xiàn)646條經(jīng)驗(yàn) 獲得超225個(gè)贊
哪里需要用到冪啊,設(shè)置一個(gè)計(jì)數(shù)器,初始化為0,每次將該數(shù)除以10并賦值給該數(shù),計(jì)數(shù)器加1,直到該數(shù)為0.最后計(jì)數(shù)器的值即為該數(shù)的位數(shù)。
添加回答
舉報(bào)