1凰盔、假如投資的年利率為5%,試求從1000塊增長到5000塊,需要花費多少年
1000 1000+10000.05=10001.05
1050 1050+10500.05=10501.05
……
5000
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>循環(huán)練習(xí)2</title>
<script type="text/javascript">
var money = 1000;
var count = 0;
while(money<5000){
money *=1.05;
count++;
}
alert ("一共需要"+count+"年");
</script>
</head>
<body>
</body>
</html>
2灾杰、用while循環(huán)重寫小明的成績,如果用戶輸入不合法就反復(fù)輸入熙参,直到正確為止
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>if練習(xí)1</title>
<script type="text/javascript">
while(true){
var score = prompt("請輸入小明的期末成績(0-100:")
if(score>=0&&score<=100){
break;
} alert("請輸入有效的分?jǐn)?shù)")}
if (score == 100){
alert("獎勵一輛BMW");}
else if(score<=99 && score>=80){
alert("獎勵一臺iphone15s");}
else if(score<80 && score>=60){
alert("獎勵一本參考書");}
else{
alert("什么獎勵也沒有");}
</script>
</head>
<body>
</body>
</html>
對于成績大于等于60分的艳吠,輸出'合格'。低于60分的孽椰,輸出'不合格'
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>switch練習(xí)1</title>
<script type="text/javascript">
var score = prompt("請輸入成績(0-100):")
switch(true){
case(score>=60 && score<=100):
alert("合格");
break
case(score==""):
alert("不能為空");
break
case (score2>=0&&score2<60):
alert("不合格");
break
default:
alert("輸入不合法");
break
}
</script>
<body>
</body>
</html>
4.從鍵盤接收整數(shù)參數(shù)昭娩,如果該數(shù)為1-7,打印對應(yīng)的星期黍匾,否則打印非法參數(shù)栏渺。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>switch練習(xí)2</title>
<script type="text/javascript">
var week=prompt("請輸入1-7的數(shù)字")
var weekday=Number(week)
switch (weekday) {
case 1:
alert("星期一")
break ;
case 2:
alert("星期二")
break ;
case 3:
alert("星期三")
break;
case 4:
alert("星期四")
break;
case 5:
alert("星期五")
break;
case 6:
alert("星期六")
break ;
case 7:
alert("星期日")
break ;
default:{
alert("輸入錯誤") }}
</script>
<body>
</body>
</html>
window.onload = function () {
var value1 = document.getElementById("value1");
var value2 = document.getElementById("value2");
var value3 = document.getElementById("value3");
var btn = document.getElementById("btn");
var select = document.getElementById("select")
btn.onclick = function () { var one, two, bignum;
try { one = value1.value.split(".")[1].length }
catch (e) {
one = 1
}
try {
two = value2.value.split(".")[1].length
} catch (e) {
two = 1 }
bignum = Math.pow(10, Math.max(one, two))
if (value1.value == "" || value2.value == "") {
alert('值不能為空')
value1.value = "";
value2.value = "";
value3.value = ""; }
else if ((value1.value != parseFloat(value1.value)) || (value2.value !=parseFloat(value2.value))) {
alert("輸入的值有誤")
value1.value = "";
value2.value = "";
value3.value = ""; }
else { if (select.value == "+") {
value = (parseFloat(value1.value) * bignum + parseFloat(value2.value) * bignum) / bignum value3.value = value }
else if (select.value == "-")
{ value = (parseFloat(value1.value) * bignum - parseFloat(value2.value) * bignum) / bignum value3.value = value }
else if (select.value == "") { value = (parseFloat(value1.value) * bignum * parseFloat(value2.value) * bignum) / (bignum * bignum) value3.value = value } else { if (parseFloat(value2.value) == 0)
{ alert("除數(shù)不能為零")
value1.value = "";
value2.value = "";
value3.value = ""; }
else { value = ((parseFloat(value1.value) * bignum) / (parseFloat(value2.value) * bignum)) value3.value = value } } } } }
</script>
</head>
<body>
<div class="box">
<h1>簡易計算</h1>
<input type="text" id="value1">
<select id="select">
<option>+</option>
<option>-</option>
<option></option>
<option>/</option>
</select>
<input type="text" id="value2">
<input type="button" id="btn" value="=">
<input type="text" id="value3">
</div>
</body>
</html>