<%
int apps = 13;
out.println("<div>");
out.println("<table>");
out.println("<tr>");
for (int i = 1; i <= apps; i++) {
out.println("<td>");
out.println("" + i + "");
}
out.println("</td>");
out.println("</tr>");
out.println("</table>");
out.println("</div>");
%>
为此我输出为1 2 .... 13但我需要输出为
<{1}}中的 1 3 5 7 9 11 13 ,并留在one row
2 4 6 8 10 12 。我可以这样做吗?如果我有app = 100输出需要相同的fashion.inside表值必须在两行。谢谢你的帮助。
答案 0 :(得分:2)
喜欢这个
<%
int apps = 13;
out.println("<div>");
out.println("<table>");
Stringbuilder firstRow = new StringBuilder();
firstRow.append("<tr>");
Stringbuilder secondRow = new StringBuilder();
secondRow.append("<tr>");
out.println("<tr>");
for (int i = 1; i <= apps; i++) {
if(i % 2 == 1 ) {
firstRow.append("<td>"+i+"</td>")
}else {
secondRow.append("<td>"+i+"</td>");
}
}
firstRow.append("</tr>");
secondRow.append("</tr>");
out.println(firstRow.toString());
out.println(secondRow.toString());
out.println("</table>");
out.println("</div>");
%>
通过检查id奇数值,甚至可以将它附加到相应的StringBuilder对象,然后将其打印出来。
答案 1 :(得分:0)
<强>源:@JJPA 强>
尝试这个...
<%
int apps = 13;
out.println("<div>");
out.println("<table>");
Stringbuilder firstRow = new StringBuilder();
firstRow.append("<tr>");
Stringbuilder secondRow = new StringBuilder();
secondRow.append("<tr>");
out.println("<tr>");
for (int i = 1; i <= apps; i++) {
if(i % 2 == 1 ) {
firstRow.append("<td>"+i+"</td>");
}
if(i % 2 == 0 ) {
secondRow.append("<td>"+i+"</td>");
}
}
firstRow.append("</tr>");
secondRow.append("</tr>");
out.println(firstRow.toString());
out.println(secondRow.toString());
out.println("</table>");
out.println("</div>");
%>