當前位置:首頁 » 編程語言 » 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 循環結構

如下圖。。。

熱點內容
隨機啟動腳本 發布:2025-07-05 16:10:30 瀏覽:513
微博資料庫設計 發布:2025-07-05 15:30:55 瀏覽:18
linux485 發布:2025-07-05 14:38:28 瀏覽:298
php用的軟體 發布:2025-07-05 14:06:22 瀏覽:747
沒有許可權訪問計算機 發布:2025-07-05 13:29:11 瀏覽:421
javaweb開發教程視頻教程 發布:2025-07-05 13:24:41 瀏覽:675
康師傅控流腳本破解 發布:2025-07-05 13:17:27 瀏覽:229
java的開發流程 發布:2025-07-05 12:45:11 瀏覽:673
怎麼看內存卡配置 發布:2025-07-05 12:29:19 瀏覽:274
訪問學者英文個人簡歷 發布:2025-07-05 12:29:17 瀏覽:824