導航:首頁 > 編程語言 > java判斷整數位數

java判斷整數位數

發布時間:2024-12-19 09:38:57

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 自定義一個整數,計算它是幾位數

/**
* <p>Title:輸入一個0到99999之間的數,判斷是幾位數 </p>
*/
import java.io.*;
public class bit {
public static void main(String[] args)throws IOException {
// bit bit = new bit();
System.out.print("please input a number between(0-99999) ");
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
int num=Integer.parseInt(reader.readLine());
if(num>=0&&num<=99999)
{
int temp,i=0;
for ( ; num!=0; i++) {
temp=num%10;
num/=10;
System.out.print(temp+" , ");
}
System.out.print("\nthis number have "+i+" bit");
}
else
System.out.print("this number is error!");
}
}

㈢ 在Java中輸入一個整數,求他是幾位數和每個位數是多少,如果是負數或者是0怎麼辦

importjava.util.Scanner;
publicclassNumberTest{
staticScannersc=newScanner(System.in);
publicstaticvoidmain(String[]args){
while(true){
System.out.println("輸入一個數字:進行測試!");
init(Integer.parseInt(sc.nextLine().replaceAll("[^\d-]","")));
}
}
privatestaticvoidinit(inta){
intn=0,tem=1;
Stringstr="",src="";
if(a!=0){
if(a<0){
tem=a*-1;
src="負數";
}else{
tem=a;
src="正數";
}
for(inti=tem;i!=0;i/=10,n++){
str+=(i%10)+",";
}
}else{
src="為0,無意義!";
}
System.out.println(a+"是["+src+"];是一個["+n+"]位數;數字排列:數字低位-->高位:"+str+" ");
}
}

㈣ 使用java,輸入一個0~99999之間的任意數,判斷輸入的數是幾位數

public static void main(String[] args) {
System.out.println("請輸入一個0~99999之間的整數,再按回車鍵:");
Scanner sc = new Scanner(System.in);
try {
int n = sc.nextInt();
if (n < 0 || n > 99999) {
System.out.println("輸入的數字超出范圍!");
}
System.out.println("輸入的數字為"
+ (n < 10 ? 1 : n < 100 ? 2 : n < 1000 ? 3 : n < 10000 ? 4
: 5) + "位數");
} catch (Exception e1) {
System.out.println("輸入錯誤!");
e1.printStackTrace();
}
}

㈤ java中怎麼得到輸入的一個數字是幾位數

importjava.util.Scanner;

publicclassA{
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
System.out.println("請輸入襲一個數字");
Scannersc=newScanner(System.in);
Stringstr=sc.next();
System.out.println("您輸入的是"+str.length()+"位數");
}
}

閱讀全文

與java判斷整數位數相關的資料

熱點內容
大數據與工業的結合包括哪些領域 瀏覽:993
蘋果7鬧鍾鈴聲設置歌曲 瀏覽:168
法庭文件有哪些 瀏覽:211
zip文件怎麼刪除嗎 瀏覽:247
win1016179安裝失敗 瀏覽:457
pp助手的文件系統 瀏覽:218
設計網站橫幅廣告要哪些因素 瀏覽:991
勞務報酬認定在多少號文件 瀏覽:209
本地數據傳輸業務有什麼用途 瀏覽:375
什麼情侶app好用 瀏覽:575
旅遊攻略適合下什麼app 瀏覽:403
nodejsbusboy下載 瀏覽:106
同事不會送文件找不到地方 瀏覽:473
銀聯萬事達applepay 瀏覽:834
試玩怎麼推廣app 瀏覽:588
課設報告中數據結構寫什麼東西 瀏覽:44
男生怎麼搜到網站的 瀏覽:441
當前無網路怎麼解決 瀏覽:299
x200thinkpad升級 瀏覽:568
華為mate8幾個版本的區別 瀏覽:582

友情鏈接