在JavaScript中使用for循环按顺序显示数字
我尝试了这段代码,得到了以下输出:在JavaScript中使用for循环按顺序显示数字,javascript,for-loop,nested-loops,Javascript,For Loop,Nested Loops,我尝试了这段代码,得到了以下输出: <script type="text/javascript"> document.write("<table border='1' width='70%'>"); for(var a=1;a<=20;a=a+5){ document.write('<tr>'); for(var b=a;b<a+5;b++){ document.write("<td&g
<script type="text/javascript">
document.write("<table border='1' width='70%'>");
for(var a=1;a<=20;a=a+5){
document.write('<tr>');
for(var b=a;b<a+5;b++){
document.write("<td>"+b+"</td>");
}
document.write('</tr>');
}
document.write("</table>");
</script>
如何显示如下输出:
1 6 11 16
2 7 12 17
3 8 13 18
4 9 14 19
5 10 15 20
也许在你开口之前你必须先试试, 这是我的解决方案,您可以尝试此代码或制作自己的代码
<script type="text/javascript">
document.write("<table border='1' width='70%'>");
// I made a condition where a <= 5. because there are 5 columns needed right there.
for(var a=1;a<=5;a++){
document.write('<tr>');
// on this loop I made a condition where b <= 20 and the increment b+=5 because every rows show a number multiple 5 start from "a" value to 20
for(var b=a;b<=20;b+=5){
document.write("<td>"+b+"</td>");
}
document.write('</tr>');
}
document.write("</table>");
</script>
脚本上的描述。我不擅长解释,抱歉,如果我的解释有点混乱在不解释错误的情况下给OP一个解决方案实际上无助于OP作为程序员的发展。是的,你是对的,抱歉,我的不好。让我改变我的答案,永远不要使用document.write。永远不要使用document.write只写开头或结尾标记的部分元素。
<script type="text/javascript">
document.write("<table border='1' width='70%'>");
// I made a condition where a <= 5. because there are 5 columns needed right there.
for(var a=1;a<=5;a++){
document.write('<tr>');
// on this loop I made a condition where b <= 20 and the increment b+=5 because every rows show a number multiple 5 start from "a" value to 20
for(var b=a;b<=20;b+=5){
document.write("<td>"+b+"</td>");
}
document.write('</tr>');
}
document.write("</table>");
</script>