使用prompt输入一个正整数n,以表格的形式在网页上输出1-n的平方和立方。如输入5,显示
n |
1 |
2 |
3 |
4 |
5 |
n2 |
1 |
4 |
9 |
16 |
25 |
n3 |
1 |
8 |
27 |
64 |
125 |
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>test3_1</title>
- <script type="text/javascript" src="out_put.js">
- function input()
- {
- do
- {
- n = prompt("请输入n的值!","");
- if(n == null)
- {
- return false;
- }
- if(n.replace(/^\s+|\s+$/g,"") == "")
- {
- alert("输入内容为空!");
- }
- }
- while (n.replace(/^\s+|\s+$/g,"") == "" && n != null);
- }
- input();
- var m; //定义行
- setrc(3,n); //调用指定行列输出表格,根据内容数组维来指定几行几列
- function setrc(m,n)
- { //指定行列输出表格函数
- if(n.replace(/^\s+|\s+$/g,"") != "" && n != null)
- {
- document.write("<table border=2 bgcolor=green align=center cellpadding=10% width=500 >")
- for(i=1;i<=m;i++)
- {
- document.write("<tr align=center >")
- if(i==1)
- {document.write("<td>n")}
- if(i==2)
- {document.write("<td>n<sup>2</sup>")}
- if(i==3)
- {document.write("<td>n<sup>3</sup>")}
- for(j=1;j<=n;j++)
- document.write("<td>"+Math.pow(j,i)) //输出数组
- }
- document.write("</table>")
- }
- else
- return false;
- }
- </script>
- </head>
- <body>
- </body>
- </html>
效果如下: