當前位置:首頁 » 編程語言 » java判斷數字位數字

java判斷數字位數字

發布時間: 2022-10-11 13:00:59

java如何判斷數字的位數

按照num/10 語句,不是應該顯示 它是個99位數嗎?java是如何判斷為3位數的?

答999/10=99;此時num=99count=1;
99/10=9;此時num=9count=2;
9/10=0;此時num=0count=3;
一共在while循環里執行了三次,所以判斷是3位數

提示

System.out.println("它是個"+count+"位的數!");
這里輸出的是count這個變數,表達的是次數,
不是輸出num這個數,此時num經過循環已經等於0了

② 用java判斷一個數的位數 怎麼寫

importjava.util.*;
publicclassDemo{
publicstaticvoidmain(String[]args)throwsException{
Scannerscan=newScanner();
Stringst=scan.nextLine();
System.out.println("這個數字的位數是"+st.length());
}
}

③ Java中,任意輸入一個整數,如何判斷是幾位數

importjava.math.BigInteger;

publicclassTest{

//輸入的整數有限制,輸入的數必須在-2^63與2^63-1之間
publicstaticvoidlengthOfInt(longl){
Strings=l+"";
if(l<0)
System.out.println(s.length()-1);
else
System.out.println(s.length());

}

//任意長度的整數
publicstaticvoidlengthOfInt(BigIntegerbigInt){
Strings=newString(bigInt+"");
if(s.startsWith("-"))
System.out.println(s.length()-1);
else
System.out.println(s.length());

}

publicstaticvoidmain(String[]args){
lengthOfInt(11000);
lengthOfInt(newBigInteger("-"));

}
}

④ 在Java中對於給定的任意的正整數,判斷這個數是幾位數並輸出結果。要求使用do while循環結構

第一種從個位向前方式:
Integer num = 123456789;//正數
int index = 0;
do {
System.out.println(num % 10);
num =num / 10;
index ++ ;
} while (num > 0);

System.out.println(index);//這個正整數為幾位數
第二種從前向後輸方式:
Integer num = 123456789;//正數
String numStr = num+"";//先轉為字元串
int index = 0 ;//設定下標
do {
System.out.println(numStr.charAt(index));//根據下標取出對應的字元輸出
index++;//下標加一
} while (index < numStr.length());//條件下標需小於字元串的長度
System.out.println(numStr.length());//這個正整數為幾位數

⑤ 在Java中,對於給定正整數,判斷這個數是幾位數,並輸出結果,要求使用do while 循環結構

如下圖。。。

熱點內容
java返回this 發布:2025-10-20 08:28:16 瀏覽:585
製作腳本網站 發布:2025-10-20 08:17:34 瀏覽:881
python中的init方法 發布:2025-10-20 08:17:33 瀏覽:574
圖案密碼什麼意思 發布:2025-10-20 08:16:56 瀏覽:761
怎麼清理微信視頻緩存 發布:2025-10-20 08:12:37 瀏覽:678
c語言編譯器怎麼看執行過程 發布:2025-10-20 08:00:32 瀏覽:1005
郵箱如何填寫發信伺服器 發布:2025-10-20 07:45:27 瀏覽:251
shell腳本入門案例 發布:2025-10-20 07:44:45 瀏覽:108
怎麼上傳照片瀏覽上傳 發布:2025-10-20 07:44:03 瀏覽:799
python股票數據獲取 發布:2025-10-20 07:39:44 瀏覽:705