Java中怎么确定输入的数是几位数

Python016

Java中怎么确定输入的数是几位数,第1张

复制以下代码:

int num = 2772

String str = String.valueOf(num)

System.out.println(str.length())

for(char c : str.toCharArray()){

System.out.println(c)

}

扩展资料:

字符串的长度

public class Test {

public static void main(String[] args) {

int count = 0

String regex = "[\u4e00-\u9fa5]"

String str = "今天阳光明媚不是吗."

Pattern p = Pattern.compile(regex)

Matcher m = p.matcher(str)

System.out.print("提取出来的中文有:")

while (m.find()) {

count++

System.out.print(m.group() + " ")

}

System.out.println()

System.out.println("汉字出现的频率:" + count)

}

}

import java.math.BigInteger

public class Test {

    //输入的整数有限制,输入的数必须在-2^63与2^63 - 1之间

    public static void lengthOfInt(long l){

        String s = l + ""

        if(l < 0)

            System.out.println(s.length() - 1)

        else

            System.out.println(s.length())

    }

    //任意长度的整数

    public static void lengthOfInt(BigInteger bigInt){

        String s = new String(bigInt + "")

        if(s.startsWith("-"))

            System.out.println(s.length() - 1)

        else

            System.out.println(s.length())

    }

    public static void main(String[] args){

        lengthOfInt(11000)

        lengthOfInt(new BigInteger("-1100000000000000000000000000000000000000"))

    }

}

其中的大小也就是长度的位,电脑保存数据是保存二进制“01”数据,一个0或1占一位,整型int数据长度32位,就是32个二进制数字它能表示的数字就是值为 -2^31-2^31-1.