导航:首页 > 编程语言 > 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判断整数位数相关的资料

热点内容
fgets空文件 浏览:857
传奇物品文件夹 浏览:352
word插入excel图表 浏览:690
xp系统搜索不到本地文件 浏览:39
什么网站可以找到拼团去西藏 浏览:247
javatask 浏览:50
MFC的文件名 浏览:972
cad自动保存的文件怎么关闭 浏览:667
zip文件密码多少 浏览:486
编程培训哪些好 浏览:324
如何把文件夹内容变成文档 浏览:509
小火箭幼儿编程怎么拿积分 浏览:158
火车票时刻表及票价数据库 浏览:94
求平均值java 浏览:767
linux如何分屏显示 浏览:257
手机可以直接填写pdf文件么 浏览:554
linux虚拟光驱软件 浏览:836
米2s最好的版本 浏览:640
小米6检测工具下载 浏览:540
桌面创建不了文件夹怎么回事 浏览:894

友情链接