<html>
<head>
<title>while语句</title>
</head>
<body>
<h1>while语句应用举例</h1>
<hr>
<form id="form1">
<input type="text" id="Num1" name="Num1" size="15" value="请在这里输入行数">
<input type="text" id="Num2" name="Num2" size="15" value="请在这里输入列数">
<input type="button" value="列表格" οnclick="NumSubmit();">
</form>
<script language="JavaScript">
function NumSubmit()
{
var InputNum1 = form1.Num1.value;
var InputNum2 = form1.Num2.value;
if(isNaN(InputNum1)||isNaN(InputNum2))
{
alert("行或列不是数值!");
}
else
{
if(InputNum1==""||InputNum2=="")
{
alert("行或列输入不能为空!");
}
else{
showTable(InputNum1, InputNum2);
}
}
}
function showTable(Numb1, Numb2)
{
var Numb1, Numb2;
var m=0, n=0;
if(Numb1<1||Numb2<1)
{
alert("行或列输入值太小!");
}
if(Numb1>100||Numb2>100)
{
alert("行或列输入值太大!");
}
else
{
document.write("<head><title>while循环示例</title></head>");
document.write("使用while循环语句画出的");
document.write(Numb1+"行"+Numb2+"列的表格:<br><br>");
document.write("<table border='1' bordercolor='green'");
document.write("cellpadding = '10' cellspacing='10'>");
while(m<Numb1)
{
document.write("<tr>");
while(n<Numb2)
{
document.write("<td align='center'>第"+m+1+"行第"+n+1+"列</td>"); // 注意!!! 若不加括号,m,n,1均被当作字符串处理
n++;
}
document.write("</tr>");
m++;
n=0;
}
}
document.write("</table>");
}
</script>
</body>
</html>
更改后:
<html>
<head>
<title>while语句</title>
</head>
<body>
<h1>while语句应用举例</h1>
<hr>
<form id="form1">
<input type="text" id="Num1" name="Num1" size="15" value="请在这里输入行数">
<input type="text" id="Num2" name="Num2" size="15" value="请在这里输入列数">
<input type="button" value="列表格" οnclick="NumSubmit();">
</form>
<script language="JavaScript">
function NumSubmit()
{
var InputNum1 = form1.Num1.value;
var InputNum2 = form1.Num2.value;
if(isNaN(InputNum1)||isNaN(InputNum2))
{
alert("行或列不是数值!");
}
else
{
if(InputNum1==""||InputNum2=="")
{
alert("行或列输入不能为空!");
}
else{
showTable(InputNum1, InputNum2);
}
}
}
function showTable(Numb1, Numb2)
{
var Numb1, Numb2;
var m=0, n=0;
if(Numb1<1||Numb2<1)
{
alert("行或列输入值太小!");
}
if(Numb1>100||Numb2>100)
{
alert("行或列输入值太大!");
}
else
{
document.write("<head><title>while循环示例</title></head>");
document.write("使用while循环语句画出的");
document.write(Numb1+"行"+Numb2+"列的表格:<br><br>");
document.write("<table border='1' bordercolor='green'");
document.write("cellpadding = '10' cellspacing='10'>");
while(m<Numb1)
{
document.write("<tr>");
while(n<Numb2)
{
document.write("<td align='center'>第"+(m+1)+"行第"+(n+1)+"列</td>");
n++;
}
document.write("</tr>");
m++;
n=0;
}
}
document.write("</table>");
}
</script>
</body>
</html>