計算年利率
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>年利率</title>
</head>
<script type="text/javascript">
//定義一個變量,作為當前的錢數(shù)
var money = 1000;
//定義一個變量驯绎,作為計數(shù)器
var count = 0;
//寫一個while循環(huán)來計算每年的錢數(shù)
while(money<5000){
money*=1.05;
count++; //計數(shù)器自增
}
document.write("一共需要"+count+"年");
</script>
<body>
</body>
</html>
小明成績改良版
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>while練習(xí)2</title>
<script type="text/javascript">
// while循環(huán)重寫小明的成績消别,如果用戶輸入不合法就反復(fù)輸入救赐,直到正確為止
while (true){
var score = prompt('請輸入小明的成績');
if (!+score || (+score*10%5 != 0) || +score>100 || +score<0) {
alert('非法字符财著,請重新輸入!');
}
else if(score<=59){
alert("翻滾吧危队!")
}
else if(score>=60 && score<=80){
alert('獎勵手機');
break;
}
else if(score>=81 && score<=99){
alert('獎勵寶馬車')
break;
}
else if(score==100){
alert('獎勵別墅一棟')
break;
}
else{
break;
}
}
</script>
<body>
</body>
</html>