<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>while练习1</title>
<script type="text/javascript">
/*
假如投资年利率为5%,试求从1000块增长到5000块,需要话费多少年
*/
var money = 1000;
var year = 0;
while (money<5000){
money*=1.05;
year++;
}
alert(year);
</script>
<body>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>while练习2</title>
<script type="text/javascript">
/*
while循环重写小明的成绩,如果用户输入不合法就反复输入,直到正确为止
*/
while (true){
var score = prompt('请输入小明的成绩');
if (!+score || (+score*10%5 != 0) || +score>100 || +score<0) {
alert('非法输入');
}else {
break;
}
}
</script>
<body>
</body>
</html>