有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在jsp代码中编写td。我已经写了结果,但是合并了

<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Insert title here</title>
        <%@ page import="java.sql.*" %>
        <%@ page import= "myExam.quest" %>
    </head>
    <body>
        <%! int index=0; %> 
        <%! int pos=0; %>   

        <% String c,a;
            String[] CA =(String[])session.getAttribute("CA");
            String[] AS =(String[])session.getAttribute("AS");
            List<quest> l=(List<quest>)session.getAttribute("myquestion");
            quest[] question = new quest[l.size()];
            l.toArray(question);
        %>
        <table border=1>
            <tr>
                <th  colspan="2" width="500">Questions</th>
                <th>CorrectAnswer</th>
                <th>Answer sheet</th>
            </tr>
            <tr>
                <% 
                    for(pos=0;pos<question.length;pos++){
                    String ques=question[pos].ques;
                    String opa=question[pos].opa;
                    String opb=question[pos].opb;
                    String opc=question[pos].opc;
                    String opd=question[pos].opd;
                    c=CA[pos];
                    a=AS[pos];
                %>  
                <td>Question <%=pos+1%></td>
                <td width="500"><%= ques %></td>
                <td><%=c  %></td>
                <td><%= a%>
            </tr>
        </table>
        <%} %>
    </body>
</html>

我写的每一件事都是写请忽略会议

结果是

output of the program 只有第一行是表格格式

请建议一些要执行的工作或如何修改它


共 (2) 个答案

  1. # 1 楼答案

    问题是您的表html没有很好地嵌套在循环中

     <% 
     for(pos=0;pos<question.length;pos++){
     String ques=question[pos].ques;
     String opa=question[pos].opa;
     String opb=question[pos].opb;
     String opc=question[pos].opc;
     String opd=question[pos].opd;
     c=CA[pos];
     a=AS[pos];
    %>
       <tr> <!  moved inside  >
        <td>Question <%=pos+1%></td>
        <td width="500"><%=ques%></td>
        <td><%=c%></td>
        <td><%=a%></td> <!  added missing closing tag  >
       </tr>
    <%} %>
    </table><!  moved outside  >
    
  2. # 2 楼答案

    使用scriplets编写代码是一种非常糟糕的做法。你应该避免使用涂鸦。无论如何,请尝试以下代码:

    在此处插入标题
        <% String c,a;
            String[] CA =(String[])session.getAttribute("CA");
            String[] AS =(String[])session.getAttribute("AS");
            List<quest> l=(List<quest>)session.getAttribute("myquestion");
            quest[] question = new quest[l.size()];
            l.toArray(question);
        %>
        <table border=1>
            <tr>
                <th  colspan="2" width="500">Questions</th>
                <th>CorrectAnswer</th>
                <th>Answer sheet</th>
            </tr>
            <% 
                for(pos=0;pos<question.length;pos++){
                String ques=question[pos].ques;
                String opa=question[pos].opa;
                String opb=question[pos].opb;
                String opc=question[pos].opc;
                String opd=question[pos].opd;
                c=CA[pos];
                a=AS[pos];
            %>
                <tr><!  moved tr inside for loop >
                    <td>Question <%=pos+1%></td>
                    <td width="500"><%= ques %></td>
                    <td><%=c  %></td>
                    <td><%= a%></td><!  added missing /td >
                </tr>
            <%} %>
        </table><!  moved /table outside of for loop >
    </body>