为什么输出中有负数?

Why there are negatve numbers in the output?

任务很经典:计算 30 个月后兔子对的数量,考虑到每个成年配对给三对幼兔。 接下来是代码:

    int young = 1;
    int mature = 0;  
    for(int n=2; n<=31; n++)
    {
        int take_away=young;
        young=3*mature; 
        mature=mature+take_away;
        System.out.println("month:"+n+"\t"+"mature\t"+mature+"\t"+"young\t"+young+"\n");
        if(n==31)System.out.println(mature+take_away);
    }

这是问题所在:

最初一切都很好:

月:2成熟1年轻0

月:3成熟1幼3

月:4成熟4年轻3

月:5成熟7年轻12

从第 28 个月开始输出如下:

月:28成熟1674257764年轻-2113786333

负数从何而来?

您使用 int,新号码超过 Integer.MAX_VALUE。请改用 long