zoukankan      html  css  js  c++  java
  • js基本语法2

    编写一个程序,可以接收数据,实现求和12+121

    writeln()向文档写HTML表达式或JavaScript代码

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title></title>
            <script language="JavaScript">
    var val=window.prompt("请输入值");
    var val1=window.prompt("再输入一个值");
    document.writeln("你的输入是:"+(val+val1));
            </script> 
        </head>
        <body>
        </body>
    </html>
    View Code

    preseFloat转化一下就可以

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title></title>
            <script language="JavaScript">
    var val=window.prompt("请输入值");
    var val1=window.prompt("再输入一个值");
    document.writeln("你的输入是:"+(parseFloat(val)+parseFloat(val1)));
            </script> 
        </head>
        <body>
        </body>
    </html>
    View Code

     案例二:关系运算符

    window.prompt 与document.writeln()

    实现可以接收两个数(整数或者小数),判断大小

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title></title>
            <script language="JavaScript">
    var val=window.prompt("请输入值");
    var val1=window.prompt("再输入一个值");
    val=parseFloat(val);
    val1=parseFloat(val1);
    if(val>val1){
        window.alert("val>val1");
    }else if(val==val1){
        window.alert("val=val1");
    }else{
            window.alert("val<val1");
    }
            </script> 
        </head>
        <body>
        </body>
    </html>
    View Code

     案例三:

    逻辑运算符 与或非

    0 false null undefined NHN 都为false

    a=332

    b=321

    d=0

    c=a||b    为332 

    c=d||a  为332  取第一个不为false的数值,都为false的话则为false

    或返回的返回甚至可以为一个对象

    位运算与移位运算与java一样

    二、三大流程控制

    顺序:

    <!DOCTYPE html>
    <html>
    
        <head>
            <meta charset="UTF-8">
            <title></title>
            <script language="JavaScript">
                for(var i=0;i<10;i++){
                    document.writeln("大佬你好啊!");
                }
            </script>
        </head>
    
        <body>
        </body>
    
    </html>
    View Code

    分支:

    单分支if  双分支if else 多分支 if else if  else 

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title></title>
            <script language="JavaScript">
    var age=19;
    var sex=window.prompt("输入性别");
    if(sex=="") window.alert("去男厕所");
    else if(sex==""){
        window.alert("女厕所");
    }else{
        window.alert("不上厕所");
    }
            </script> 
        </head>
        <body>
        </body>
    </html>
    View Code

    当找到一个分支满足则跳出这个分支结构

    switch:

    <!DOCTYPE html>
    <html>
    
        <head>
            <meta charset="UTF-8">
            <title></title>
            <script language="JavaScript">
                var age = 890;
                switch(age) {
                    case 90:
                        window.alert("大佬好1");
                        break;
                    case 890:
                        window.alert("美女好");
                        break;
                    default:
                        break;
                }
            </script>
        </head>
    
        <body>
        </body>
    
    </html>
    View Code

    循环:

    for循环 while循环(先判断再执行) do-while(先执行再判断)

    <!DOCTYPE html>
    <html>
    
        <head>
            <meta charset="UTF-8">
            <title></title>
            <script language="JavaScript">
            /*输出helllo,world十次*/
            var i=0;
            while(i<10){
                document.write("hello,world<br/>");
            i++;
            }
            i=0;
        do{
                document.write("加油中国!<br/>");
                i++;
        }while (i<10)
            </script>
        </head>
    
        <body>
        </body>
    
    </html>
    View Code

    其他都可以认为是真

    alter与document.writeln的区别:

    首先,alert是一个自动弹出对话框的警告性方法,只能向用户显示程序员希望展示给用户的某些信息,在用户确定知道信息以后,点击确定,关闭对话框。

    而document.write()的功能是完成数据的输出,具体来说就像C语言中的printf,可以把它理解为输出函数,可以向用户输出各种数据,而又不仅仅是用对话框和对话框里面的简单文本字符串的来展现。

  • 相关阅读:
    Security and Cryptography in Python
    Security and Cryptography in Python
    Security and Cryptography in Python
    Security and Cryptography in Python
    Security and Cryptography in Python
    Security and Cryptography in Python
    Security and Cryptography in Python
    微信小程序TodoList
    C语言88案例-找出数列中的最大值和最小值
    C语言88案例-使用指针的指针输出字符串
  • 原文地址:https://www.cnblogs.com/helloworld2019/p/10915284.html
Copyright © 2011-2022 走看看