<!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>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head><body>
<script type="text/javascript">for(var i=1;i<6;i++)
{
document.write("<table width=300 border=1>");
        document.write("<tr>");
        document.write("<td>");

var a=Math.floor(Math.random()*100+1);
var b=Math.floor(Math.random()*100+1);
document.write(a+"加"+b+"=");
function check()

var x=document.getElementsByName("text"+i+"")[0];
var c=x.value;
if(a+b==c)
{
document.write("正确");
}
else
{
document.write("错误");
}
}

document.write("<input name='text"+i+"' type='text' size='5' />");
          document.write("<input type='button' name='button' value='查看对错' onclick='return check();' />");
document.write("</td>");
        document.write("</tr>");
    document.write("</table>");
}
   </script>
   <script type="text/javascript">
   for(var i=0;i<5;i++)
{
  document.write("<table width=300 border=1>");
       document.write("<tr>");
         document.write("<td>");
 

var a1=Math.floor(Math.random()*100+1);
var b1=Math.floor(Math.random()*100+1);
while(b1>a1)
{
var a1=Math.floor(Math.random()*100+1);
var b1=Math.floor(Math.random()*100+1);
}
document.write(a1+"减"+b1+"=");
function check2()

var x1=document.getElementsByName("text2")[0];
var c1=x1.value;
if(a1-b1==c1)
{
document.write("正确");
}
else
{
document.write("错误");
}
}
document.write("<input name='text2' type='text' size='5' />");
         document.write  ("<input type='button' name='button2' value='查看对错' onclick='return check2();' />");
document.write ("</td>");
      document.write ("</tr>");
    document.write ("</table>");
}
</script>

  </tr>
</table></body>
</html>

解决方案 »

  1.   

    两个明显的问题,以加距离
    1、c返回的类型是string,因此需要parseInt转换;
    2、最大的问题,循环后,a、b值都是最后一次循环的值,因此除了最后一个加法等式,其余永远的都不对
      

  2.   

    <!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>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    </head><body>
    <script type="text/javascript">for(var i=1;i<6;i++)
    {
    document.write("<table width=300 border=1>");
      document.write("<tr>");
      document.write("<td>");var a=Math.floor(Math.random()*100+1);
    var b=Math.floor(Math.random()*100+1);
    document.write(a+"加"+b+"=")
    document.write("<input name='text"+i+"' type='text' size='5' />");
      document.write("<input type='button' name='button' value='查看对错' onclick='return check("+i+");' />");
    document.write("</td>");
      document.write("</tr>");
    document.write("</table>");
    }function check(i)

    var x=document.getElementsByName("text"+i+"")[0];
    var c=x.value;
    if(a+b==c)
    {
    document.write("正确");
    }
    else
    {
    document.write("错误");
    }
    }
    </script></body>
    </html>
      

  3.   

    给你个实例吧,动态的自己加<!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>
        <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
        <title></title>
        <style type="text/css">
            table { float: left; }
            td { height: 30px; }
        </style>    <script type="text/javascript">
            function compute() {
                var t1 = document.getElementById("table1");
                var tds = t1.getElementsByTagName("td");
                var btns = document.getElementsByName("btn");
                for (var i = 0; i < tds.length; i++) {
                    var a = Math.floor(Math.random() * 100 + 1);
                    var b = Math.floor(Math.random() * 100 + 1);
                    t1.cells[i].innerText = a + "+" + b;
                    btns[i].onclick = function() {
                        var txts = document.getElementsByName("txt");
                        var btns = document.getElementsByName("btn");
                        for (var i = 0; i < txts.length; i++) {
                            if (btns[i] == this) {
                                if (txts[i].value == eval(t1.cells[i].innerText)) {
                                    alert("正确");
                                }
                                else {
                                    alert("错误");
                                }
                            }
                        }
                    }
                }
            }
        </script></head>
    <body onload="compute()">
        <table id="table1" border="1">
            <tr>
                <td></td>
            </tr>
            <tr>
                <td></td>
            </tr>
            <tr>
                <td></td>
            </tr>
            <tr>
                <td></td>
            </tr>
        </table>
        <table id="table2" border="1">
            <tr>
                <td>
                    <input type="text" name="txt" size="5" /><input type="button" name="btn" value="计算" />
                </td>
            </tr>
            <tr>
                <td>
                    <input type="text" name="txt" size="5" /><input type="button" name="btn" value="计算" />
                </td>
            </tr>
            <tr>
                <td>
                    <input type="text" name="txt" size="5" /><input type="button" name="btn" value="计算" />
                </td>
            </tr>
            <tr>
                <td>
                    <input type="text" name="txt" size="5" /><input type="button" name="btn" value="计算" />
                </td>
            </tr>
        </table>
    </body>
    </html>
      

  4.   

    说实话这个真绕,大半天才写出来了,ff的兼容性我没写,自己弄下就行了,或者全部改成jQuery