Java中int的范围 [英] The range of int in Java

查看:79
本文介绍了Java中int的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 Java 中的 int 范围应该是 -2^31 到 2^31-1.

I understand that the int range in Java should be -2^31 to 2^31-1.

但是当我用 20 运行这段代码时:

But when I run this code snippet with 20:

public class Factorial {
    public int factorial(int n) {
        int fac=1;
        for (int i=1; i<=n; i++) {
            fac *= i;
            System.out.println("Factorial of " + i + " is: " + fac);
        }
        return fac;
    }
}

输出:

Factorial of 1 is: 1
Factorial of 2 is: 2
Factorial of 3 is: 6
Factorial of 4 is: 24
Factorial of 5 is: 120
Factorial of 6 is: 720
Factorial of 7 is: 5040
Factorial of 8 is: 40320
Factorial of 9 is: 362880
Factorial of 10 is: 3628800
Factorial of 11 is: 39916800
Factorial of 12 is: 479001600
Factorial of 13 is: 1932053504
Factorial of 14 is: 1278945280
Factorial of 15 is: 2004310016
Factorial of 16 is: 2004189184
Factorial of 17 is: -288522240
Factorial of 18 is: -898433024
Factorial of 19 is: 109641728
Factorial of 20 is: -2102132736

从 13 (13! = 6,227,020,800) 开始就没有意义了.看起来它超出了范围并被缠绕.怎么了?是因为我正在使用 Eclipse 吗?

It's not making sense from 13 (13! = 6,227,020,800). It looks like it's out of range and wrapped around. What's wrong? Is it due to Eclipse that I'm using?

虽然我认为它不相关,但这里是测试代码:

Though I think it's not relevant, here is the test code:

public class TestFac {

    public static void main(String[] args) {
        int n;
        Scanner sc = new Scanner(System.in);

        System.out.println("Input num you want to factorial: ");
        n = sc.nextInt();
        Factorial fac = new Factorial();
        fac.factorial(n);
    }
}

推荐答案

这里想提一下整数时钟的概念.

Here I would like to mention the concept of integer clock.

Java 中 int 的最大值和最小值是:

The maximum and minimum values for int in Java are:

int MAX_VALUE = 2147483647
int MIN_VALUE = -2147483648

请检查以下结果

 int a = 2147483645;
 for(int i=0; i<10; i++) {
    System.out.println("a:" + a++);
 }

输出:

a:2147483645
a:2147483646
a:2147483647
a:-2147483648
a:-2147483647
a:-2147483646
a:-2147483645
a:-2147483644
a:-2147483643
a:-2147483642

表示当你超出整数+ve范围的限制时,下一个值又从它的负起始值开始.

It shows that when you go beyond the limit of the +ve range of integer, the next values starts from its negative starting value again.

 -2147483648,       <-----------------
 -2147483647,                        |
 -2147483646,                        |
  .                                  |
  .                                  |
  .                                  |    (the next value will go back in -ve range)
  0,                                 |
 +1,                                 |
 +2,                                 |
 +3,                                 |
  .                                  |
  .                                  |
  .,                                 |
 +2147483645,                        |
 +2147483646,                        |
 +2147483647     ---------------------

如果计算 13 的阶乘,则为 6227020800.这个值超出了java的int范围.所以新值将是

If you calculate the factorial of 13 it is 6227020800. This value goes beyond the int range of java. So the new value will be

        6227020800
      - 2147483647 (+ve max value)
   -----------------
Value = 4079537153
      - 2147483648 (-ve max value)
   -----------------
value = 1932053505
   -             1  (for zero in between -ve to +ve value)
  ----------------
Answer = 1932053504

因此,在您的回答中,13 的阶乘变为 1932053504.这就是整数时钟的工作原理.

So, in your answer, the factorial of 13 is becoming 1932053504. This is how integer clock works.

您可以使用 long 数据类型代替整数来实现您的目的.

You can use long datatype instead of integer to achieve your purpose.

这篇关于Java中int的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆