练习

作者: 小袋鼠cf | 来源:发表于2018-09-17 20:42 被阅读0次

    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>循环练习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>

    1.jpg

    2、用while循环重写小明的成绩,如果用户输入不合法就反复输入,直到正确为止

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8" />
    <title>if练习1</title>
    <script type="text/javascript">
    while(true){
    var score = prompt("请输入小明的期末成绩(0-100:")
    if(score>=0&&score<=100){
    break;
    } alert("请输入有效的分数")}
    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>


    360截图20180917204119982.jpg 360截图20180917204129630.jpg

    对于成绩大于等于60分的,输出'合格'。低于60分的,输出'不合格'

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>switch练习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>


    360截图20180917215057944.jpg
    360截图20180917215106299.jpg

    4.从键盘接收整数参数,如果该数为1-7,打印对应的星期,否则打印非法参数。
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>switch练习2</title>
    <script type="text/javascript">
    var week=prompt("请输入1-7的数字")
    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>

    360截图20180917215540604.jpg
    360截图20180917215549392.jpg

    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("除数不能为零")
    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>

    相关文章

      网友评论

          本文标题:练习

          本文链接:https://www.haomeiwen.com/subject/studnftx.html