天天看點

js用while循環計算假如投資多年的利率為5%,試求從1000塊增長到5000塊,需要花費多少年

代碼如下:

<!DOCTYPE html>
<html >
<head>
	<meta charset="UTF-8" />
	<meta name="viewport" content="width=device-width, initial-scale=1.0" />
	<meta http-equiv="X-UA-Compatible" content="ie=edge" />
	<title>假如投資多年的利率為5%,試求從1000塊增長到5000塊,需要花費多少年</title>
	<script type="text/javascript">
		var money = 1000;
		var year = 0;
		while(money <= 5000){
			money = money +(money*0.05);
			year++;
			console.log("第" + year + "年,有" + money + "元"+"<br />");
		}
	</script>
</head>
<body>
	
</body>
</html>
           

運作結果如下:

js用while循環計算假如投資多年的利率為5%,試求從1000塊增長到5000塊,需要花費多少年

或者也可以這樣:

代碼:

<!DOCTYPE html>
<html >
<head>
	<meta charset="UTF-8" />
	<meta name="viewport" content="width=device-width, initial-scale=1.0" />
	<meta http-equiv="X-UA-Compatible" content="ie=edge" />
	<title>假如投資多年的利率為5%,試求從1000塊增長到5000塊,需要花費多少年</title>
	<script type="text/javascript">
		var money = 1000;
		var year = 0;
		while(money <= 5000){
			money = money +(money*0.05);
			year++;
		}
		console.log("第" + year + "年,有" + money + "元"+"<br />");
	</script>
</head>
<body>
	
</body>
</html>
           

運作結果:

js用while循環計算假如投資多年的利率為5%,試求從1000塊增長到5000塊,需要花費多少年

繼續閱讀