如何在2 div中打印11个数字,在每个div我需要8个名字(每行4个td)第二个div从9,我该怎么办?

时间:2014-02-11 13:42:56

标签: html jsp

<%
        int apps = 11;
        int noOfDiv = apps % 3, k, m;
        for (int i = 1; i <= 2; i++) {
            out.println("<div>");
            out.println("<table>");
            for (int j = 1; j <= 2; j++) {
                out.println("<tr>");
                for (k = 1; k <= 4; k++) {
                    out.println("<td>");
                    out.println("" + k + "");
                    out.println("</td>");
                }
                out.println("</tr>");
            }
            out.println("</table>");
            out.println("</div>");
        }
    %>

为此我输出为

1234
1234  
div1中的

1234
1234  
in div2,

但我需要

1234
5678  
在div1和

9 10 11 
如果我总共有11个数字,则在div2中

2 个答案:

答案 0 :(得分:0)

这是因为您为k = 1到4打印k,并且在任何循环之前更正使用额外的变量说资本K = 1,然后替换:

for (k = 1; k <= 4; k++) // print 1 to 4  

为:

int noOfDiv = apps % 3, k, m, K = 1;   // Added K = 1
// rest of your codes ... 
for (k = K; k <= K + 3; k++){
    // code you  already have to print small `k`
    K += 4;
 }

答案 1 :(得分:0)

您可以使用以下代码执行此操作;

<%

    int apps = 11;
    int noOfDiv = apps % 3, k, m;
    for (int i = 1; i <= 2; i++) {
        out.println("<div>");
        out.println("<table>");
        for (int j = 1; j <= 2; j++) {
            out.println("<tr>");
            int temp = (j-1)*4 +1;
            for (k = temp; k <= temp+3; k++) {
                out.println("<td>");
                out.println("" + k + "");
                out.println("</td>");
            }
            out.println("</tr>");
        }
        out.println("</table>");
        out.println("</div>");


    }
    out.println("<div><table><tr><td>" + (apps - 2) + "</td><td>" + (apps - 1) + "</td><td>" + apps + "</td></tr></table></div>");

%>