4-15編程練習(xí) 十位數(shù)為什么不可以?
int num = 999;
int count = 0;
? ? for(;num!=0;){
? ? ? ?num/=10;
? ?count++;
}
System.out.println("它是個(gè)"+count+"位的數(shù)!");
我用的是MyEclipse,為何9位數(shù)以上就會(huì)報(bào)錯(cuò)?
int num = 999;
int count = 0;
? ? for(;num!=0;){
? ? ? ?num/=10;
? ?count++;
}
System.out.println("它是個(gè)"+count+"位的數(shù)!");
我用的是MyEclipse,為何9位數(shù)以上就會(huì)報(bào)錯(cuò)?
2016-03-23
舉報(bào)
2016-03-23
我理解的是,java中的int類型存儲(chǔ)長(zhǎng)度為32bit.所以范圍是“-2^32”到“2^32-1”;,也就是“-2147483648”到“2147483647”.最大是十位數(shù),但是當(dāng)大于2147483547就會(huì)溢出報(bào)錯(cuò)。
2016-03-23
int ?類型限制
2016-03-23
int num = 9999999999;
int count = 0;
while (num != 0)
{
?????? count++;
?????? num/=10;
}
你再試一試,像我這樣寫(xiě),十位是可以的,不過(guò)最大就十位,記得采納!
2016-03-23
public class Test {
?public static void main(String[] args) {
??Scanner sc = new Scanner(System.in);
??System.out.print("請(qǐng)輸入數(shù)據(jù):");
??long num =? sc.nextLong();
??int count = 0;
??for (; num != 0;) {
???num /= 10;
???count++;
??}
??System.out.println("它是個(gè)" + count + "位的數(shù)!");
?}
}