课外天地 李树青学习天地Web技术 → 课上练习代码——打印星号三角形


  共有14258人关注过本帖树形打印复制链接

主题:课上练习代码——打印星号三角形

帅哥哟,离线,有人找我吗?
admin
  1楼 博客 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信 管理员
等级:管理员 帖子:1949 积分:26803 威望:0 精华:34 注册:2003/12/30 16:34:32
课上练习代码——打印星号三角形  发帖心情 Post By: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编辑过]

 回到顶部