導航:首頁 > 編程語言 > 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判斷整數位數相關的資料

熱點內容
現在哪裡開設了編程課 瀏覽:230
漫一網路培訓學校有哪些 瀏覽:752
酷狗app如何在電腦同步播放 瀏覽:668
雲浮微信群 瀏覽:344
公司文件打不開 瀏覽:267
LOL的設置文件在哪裡 瀏覽:254
線上開庭是什麼app 瀏覽:526
新中大軟體怎麼設置文件夾 瀏覽:807
git刪除文件夾 瀏覽:353
皇室戰爭怎麼升級競技場 瀏覽:447
雅虎統計工具 瀏覽:741
edgepdf文件分類 瀏覽:901
cad為什麼捕捉不到外部參照文件 瀏覽:935
重慶一共有多少個網站 瀏覽:34
k8s配置文件env創建失敗 瀏覽:197
編程序在電腦上叫做什麼 瀏覽:92
qq閱讀可賺 瀏覽:21
怎樣查找web儲存文件圖片 瀏覽:681
人口檔案資料庫包括什麼信息 瀏覽:709
手機有什麼好玩的星戰網路游戲 瀏覽:15

友情鏈接