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