提示用户输入以创建乘法表

时间:2018-08-13 13:55:23

标签: javascript html css

该程序几乎可以正常工作,可以确保在用户输入值时将其包含在乘法表中

<html>
<head>
<title> multiplication </title>
<style> 
body{
font-family: arial;
font-size: 20px;
}
</style>
</head>
<body>
<br>
<h3> Mulitplication table </h3>
<button onclick="myFunction()">Enter number </button>
<script language ="JavaScript">
function myFunction(){
var num = prompt("Please enter your number", 0);
document.write("<center><table border ='1px'>");
for var (a =1; a < num; a++) {
document.write("<tr style='height:40px'>");
for(var b =1; b<= 10; b++) {
document.write("<td style='width:40px'><center><font size = '4'>"  + a*b + "<.center></font></td>");
}
document.write("</tr>");
}
}
document.write("</table></center>");
</script>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

您的代码中似乎有很多问题,请检查以下工作示例!

  • 以下行html标签未正确关闭,应为document.write

    document.wirite("<center><table border ='1px');
    
  • 由于您使用的是乘法表,因此每个值都需要有一行tr,以便可以将for循环更改为如下所示,中心标记也没有正确关闭!

    for (var a = 1; a < num; a++) {
        document.write("<tr><td style='width:40px'><center><font size = '4'>" + a * num + "</center></font></td></tr>");
      }
    

请参考以下示例,如果您遇到任何问题,请告诉我!

<html>

<head>
  <title> multiplication </title>
  <style>
    body {
      font-family: arial;
      font-size: 20px;
    }
  </style>
</head>

<body>
  <br>
  <h3> Mulitplication table </h3>
  <button onclick="myFunction()">Enter number </button>
  <script language="JavaScript">
    function myFunction() {
      var num = prompt("Please enter your number", 0);
      document.write("<center><table border ='1px'>");
      for (var a = 1; a < num; a++) {
        document.write("<tr><td style='width:40px'><center><font size = '4'>" + a * num + "</center></font></td></tr>");
      }
      document.write("</table></center>");
    }
  </script>
</body>

</html>