2014-03-28 26 views
1
<body> 
    <% 
     int apps = 8; 
     out.println("<div>"); 
     out.println("<table>"); 
     StringBuilder Row1 = new StringBuilder(); 
     Row1.append("<tr>"); 
     StringBuilder Row2 = new StringBuilder(); 
     Row2.append("<tr>"); 
     for (int i = 0; i < apps; i++) { 
      if (i % 2 == 0) { 
       Row1.append("<td>" + i + "</td>"); 
      } 
      if (i % 2 == 1) { 
       Row2.append("<td>" + i + "</td>"); 
      } 
     } 
     Row1.append("</tr>"); 
     Row2.append("</tr>"); 
     out.println(Row1.toString()); 
     out.println(Row2.toString()); 
     out.println("</table>"); 
     out.println("</div>"); 
    %> 
</body> 

這是我的jsp頁面。目前,我的輸出如Row1:0 2 4 6和Row2:1 3 5 7。但是我需要輸出如Row1:1 2 3 4和Row2:5 6 7 8。我怎樣才能做到這一點?如何按訂單打印兩行數字,Row1中的前四個以及Row2中的後四個數字?

+0

你只是追加甚至數行1和奇數到第2行。正確的UR邏輯 –

+0

對不起,我沒有得到你,你能糾正邏輯,我需要輸出爲Row1中的0 1 2 3和Row2中的4 5 6 7。 – tajMahal

回答

0

if條款改爲只有你的條件

for (int i = 0; i < apps; i++) { 
      if (i<4) { 
       Row1.append("<td>" + i + "</td>"); 
      } 
      if (i>3 && i<8) { 
       Row2.append("<td>" + i + "</td>"); 
      } 
     } 

輸出

Row1 :
Row2 : 4567 

工作演示:http://ideone.com/PMiknB .dint嘗試任何HTML部分

+0

感謝工作正常。 – tajMahal

相關問題