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

熱點內容
仁和數控怎麼編程 瀏覽:381
項目文件夾圖片 瀏覽:87
怎麼在東芝電視安裝app 瀏覽:954
plc顯示數字怎麼編程 瀏覽:439
如何辨別假網站 瀏覽:711
寬頻用別人的賬號密碼 瀏覽:556
新app如何佔有市場 瀏覽:42
做好的柱形圖如何插多一組數據 瀏覽:493
ios網路列印機 瀏覽:529
網路噴子現實生活狀態如何 瀏覽:223
發一份文件韻達要多少錢 瀏覽:294
iphone怎麼修改文件夾 瀏覽:694
網路小說小站有哪些 瀏覽:640
如何去除網路連接鏈條符號 瀏覽:621
地下城與勇士86版本劍豪攻略 瀏覽:551
纏中說禪最好版本 瀏覽:692
小米相冊新建文件夾怎麼刪除 瀏覽:231
唯品會源代碼 瀏覽:24
騰訊網站怎麼查詢 瀏覽:951
java中寫文件 瀏覽:940

友情鏈接