js处理数学经典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一 对兔子,假如兔子都不死,问每个月的兔子总数为多少?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript">
var num1 = 1;
var num2 = 0;
var sum = 0;
var mon = prompt("请输入你所要计算兔子数量的月数:", "1");
if(isNaN(mon) == true){
mon = parseInt(prompt(mon));
}else{
mon = parseInt(mon);
}
for(i = 1; i <= mon; i ++){
if( i % 2 != 0){
sum = num1 + num2;
num1 = sum;
}else{
sum = num1 + num2;
num2 = sum;
}
}
alert(mon + "月之后,兔子的数量为:" + 2 * sum);
</script>
</head>
<body>
</body>
</html>