为了账号安全,请及时绑定邮箱和手机立即绑定

用于字母矩阵输出的嵌套 for 循环

用于字母矩阵输出的嵌套 for 循环

三国纷争 2023-07-29 15:22:13
背景:我一直在研究嵌套 for 循环练习。我正在编写的程序旨在获取列和行数据并使用嵌套 for 循环创建字母矩阵或网格。我能够在网格中编译数据(太棒了!),但是网格顶部有一行不必要的数据。(不太好......)任何人都可以让我知道可能导致此问题的原因,我相信这是非常简单的事情,但我的大脑可能因为看了这么久而烧焦了。任何帮助将不胜感激。<!DOCTYPE html><html><head>    <title>""</title>    <script>        function letter_matrix() {            let column = parseInt(document.form.letter_column.value);            let rows = parseInt(document.form.letter_rows.value);            let text = "<table><th></th>";            let letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ";                        for (let i = 0; i < rows ; ++i) {                          text += "<tr>"+ letters.charAt(i) + "</td>";               for(let j = 0; j < column ; j++){                                   let k = (i * rows + j) % 26;                   text += "<td>"+letters.charAt(k)+ "</td>";               }                       }        text += "</table>";         document.getElementById("results").innerHTML= text;    }    </script></head><body>    <h1>""</h1>    <form name="form" action = "javascript:letter_matrix();">        Columns: <input type="text" name="letter_column">        Rows: <input type="text" name="letter_rows">        <input type="submit" value="Enter values.">    </form>    <pre id="results">    </pre></body></html>
查看完整描述

3 回答

?
哈士奇WWW

TA贡献1799条经验 获得超6个赞

在第一个文件中,for您创建一个<tr>添加一些文本并关闭一个</td>.


你很可能应该只打开一个tr,然后在内部for然后关闭tr


for (let i = 0; i < rows; ++i) {

  text += "<tr>";

  for (let j = 0; j < column; j++) {

    let k = (i * rows + j) % 26;

    text += "<td>" + letters.charAt(k) + "</td>";

  }

  text += '</tr>';

}


查看完整回答
反对 回复 2023-07-29
?
达令说

TA贡献1821条经验 获得超6个赞

您需要<tr>如下所示拆分标签。这条线引起了问题


text += "<tr>"+ letters.charAt(i) + "</td>";


您得到的异常结果只是因为这个。请参阅下面的代码,我如何在循环</tr>后使用for并删除letter.charAt(i). 这是添加了额外的行。


         text += "<tr>";

        

           for(let j = 0; j < column ; j++){

            

               let k = (i * rows + j) % 26;

        

               text += "<td>"+letters.charAt(k)+ "</td>";

           }

         text += "</tr>";


查看完整回答
反对 回复 2023-07-29
?
温温酱

TA贡献1752条经验 获得超4个赞

就是这个。 https://jsfiddle.net/Cornel777/kgbojuhz/8/

  1. 刚刚添加到"<td>"这里text += "<tr>"+ "<td>" + letters.charAt(i) + "</td>";

  2. 搬了你的<script><body>

  3. 这样做了,但这不是强制性的,只是较新的。

let column = document.getElementById("columns").value;

let rows = document.getElementById("rows").value;

 <!DOCTYPE html>

    

    <html>

    

    <head>

        <title>""</title>

    

      

    </head>

    

    <body>

        <h1>""</h1>

    

        <form name="form" action = "javascript:letter_matrix();">

    

            Columns: <input id="columns" type="text" name="letter_column">

            Rows: <input id="rows" type="text" name="letter_rows">

    

            <input type="submit" value="Enter values.">

    

        </form>

    

        <pre id="results">

        </pre>

    </body>

    

      <script>

            function letter_matrix() {

    

                let column = document.getElementById("columns").value;

                let rows = document.getElementById("rows").value;

                let text = "<table><th></th>";

    

                let letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ";

                

    

                for (let i = 0; i < rows ; i++) {

                 

                 text += "<tr>"+ "<td>" + letters.charAt(i) + "</td>";

    

                   for(let j = 0; j < column ; j++){

                   

                

                       let k = (i * rows + j) % 26;

    

                       text += "<td>"+letters.charAt(k)+ "</td>";

                   }

    

                   

            }

            

            text += "</table>";

     

            document.getElementById("results").innerHTML= text;

        }

        </script>

    

    </html>

    

查看完整回答
反对 回复 2023-07-29
  • 3 回答
  • 0 关注
  • 106 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信