package com.supersoft.exercise;
/**
* @author JamesLiu
*
* 【程序1】
* 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一
* 对兔子,假如兔子都不死,问每个月的兔子总数为多少?
* 1.程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21....
*/
import java.util.Scanner;
public class ProgramEx1 {
public static void main(String[] args) {
while (true) {
System.out.print("请输入你要计算的月数:");
Scanner scanner = new Scanner(System.in);
int month = 3;
try{
month = scanner.nextInt();
if (month<=0) {
throw new Exception();
}
}
catch (Exception e){
System.out.println("哈哈,非常不好意思你输入有错误哦,重新输入");
continue;
}
if (month<3) {
System.out.println("第一个月兔子总数为:1");
return;
}
int rabbitSum1 = 1;
int rabbitSum2 = 1;
for (int i=3; i<=month; i++) {
int temp = rabbitSum1;
rabbitSum1 = rabbitSum1 + rabbitSum2;
rabbitSum2 = temp;
System.out.println("第"+i+"个月兔子总数为:"+rabbitSum1);
}
break;
}
}
}