以文本方式查看主题

-  课外天地 李树青  (http://njcie.com/bbs/index.asp)
--  Web技术  (http://njcie.com/bbs/list.asp?boardid=28)
----  课上练习代码——打印星号三角形  (http://njcie.com/bbs/dispbbs.asp?boardid=28&id=1520)

--  作者:admin
--  发布时间:2015/6/2 10:19:45
--  课上练习代码——打印星号三角形

 版本1:

<!DOCTYPE html>

<html>

<head lang="en">

    <meta charset="UTF-8">

    <title></title>

    <script language="JavaScript">

        function getIt(line) {

            mystring = new String("");

            for (i = 0; i < parseInt(line); i++) {

                for (j = 0; j < (i + 1) * 2 - 1; j++)

                    mystring = mystring + "*";

                mystring = mystring + "<br>";

            }

            document.getElementById("result").innerHTML = mystring;

        }

    </script>

</head>

<body>

<form name="myFrm">

    输入行数:

    <input type="text" name="line" size="12"/>

    <br/>

    <input type="button" value="计算"

           onclick="getIt(document.myFrm.line.value)"/>

</form>

<div id="result" style="height:500pt;width:500pt;background-color: aquamarine;text-align:center;font-family:宋体;font-size:28pt"></div>

</body>

</html>

 

 

 版本2:

<!DOCTYPE html>

<html>

<head lang="en">

    <meta charset="UTF-8">

    <title></title>

    <script language="JavaScript">

        function getIt(line) {

            mystring = new String("");

            for (i = 0; i < line; i++) {

                for (j = 0; j < line-i-1; j++)

                    mystring = mystring + " ";

                for (j = 0; j < (i + 1) * 2 - 1; j++)

                    mystring = mystring + "*";

                mystring = mystring + "<br>";

            }

            document.getElementById("result").innerHTML = mystring;

        }

    </script>

</head>

<body>

<form name="myFrm">

    输入行数:

    <input type="text" name="line" size="12"/>

    <br/>

    <input type="button" value="计算"

           onclick="getIt(document.myFrm.line.value)"/>

</form>

<div id="result" style="height:500pt;width:500pt;background-color: aquamarine;white-space:pre;font-size:28pt"></div>

</body>

</html>

 

 

 版本3:

<!DOCTYPE html>

<html>

<head lang="en">

    <meta charset="UTF-8">

    <title></title>

    <script language="JavaScript">

        function getIt(line) {

            mystring = new String("");

            for (i = 0; i < line; i++) {

                for (j = 0; j < line-i-1; j++)

                    mystring = mystring + "&ensp;";

                for (j = 0; j < (i + 1) * 2 - 1; j++)

                    mystring = mystring + "*";

                mystring = mystring + "<br>";

            }

            document.getElementById("result").innerHTML = mystring;

        }

    </script>

</head>

<body>

<form name="myFrm">

    输入行数:

    <input type="text" name="line" size="12"/>

    <br/>

    <input type="button" value="计算"

           onclick="getIt(document.myFrm.line.value)"/>

</form>

<div id="result" style="height:500pt;width:500pt;background-color: aquamarine;font-size:28pt"></div>

</body>

</html>


此主题相关图片如下:1.png
按此在新窗口浏览图片
[此贴子已经被作者于2015-06-02 10:21:12编辑过]