首页 > 解决方案 > 绘制表格 10x10 并在表格中显示素数

问题描述

需要这样显示:

1 2 3 4 5

6 7 8 9 10

但我得到

1 2 3 4 5 6 7 8 9 10

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Table of prime numbers</title>

    <style>
            .chess-board {border-collapse: collapse; position:absolute; left: 640px; top: 240px;}
            .chess-board td { border: 1px solid; width: 2em; height: 2em; }
            .chess-board .light { background: #eee; }

        </style>

</head>
<body>

<table class="chess-board">
    <tbody>
    <%
        int i,count;
        for(int j=2;j<=541;j++)
            {
            count=0;
            for(i=1;i<=j;i++)
            {
               if(j%i==0)
               {
                    count++;
               }
            }

            if(count==2)

             %>
              <tr></tr>
              <td class="light"><%out.print(j);%></td>
            <%
            }
            %>

    </tbody>
</table>
</body>
</html>

我需要在 10x10 表中显示素数

但结果我得到了这个,一切都在一行中

标签: javajsp

解决方案


生成的 html 将如下所示:

...
<tbody>
    ...
    <tr></tr>
    <td class="light">2</td>
    <tr></tr>
    <td class="light">3</td>
    <tr></tr>
    <td class="light">4</td>
    ...
</tbody>
...

这不是有效的 html 代码。您可以在浏览器中使用“查看源代码”或“检查”以将结果查看为 html。它应该是这样的:

...
<tbody>
    ...
    <tr>
      <td class="light">2</td>
      <td class="light">3</td>
      ...
    </tr>
    <tr>
      <td class="light">12</td>
      <td class="light">13</td>
      ...
    </tr>
    ...
</tbody>

尝试修改您的代码,使其生成那种 html。...


推荐阅读