AJPFX实现兔子问题

有一对小兔子,从第三个月长成开始每个月生一对小兔子,新出生的小兔子从第三个月长成开始每个月也生一对小兔子,假设所有的兔子都不会死,问每个月兔子的总数?(月数可以是6,12).大神看看我笨方法谢的对吗?
import java.util.Scanner;
class TuZi {
        public static void main(String[] args) {
                Scanner sc=new Scanner(System.in);
                System.out.println("请输入你要计算的月数:");
                int m= sc.nextInt();
                int sum=0;
                int moon=0;
                int a=2;
                while (moon<m) {
                        if (moon<2) {
                                sum=2;
                                
                        }else if (moon>=2&&moon<=4) {
                                sum+=2;
                                
                        }else if (moon>4) {

                                sum+=2+a;
                                a+=2;
                        }
                        moon++;
                        System.out.println("第"+moon+"月的兔子总数为:"+sum);
                }

        }
}

猜你喜欢

转载自www.cnblogs.com/AJPFX/p/10876063.html