【问题标题】:If-else statement not working properlyif-else 语句不能正常工作
【发布时间】:2016-06-27 12:00:22
【问题描述】:

我的网站就像 SO。在此页面中,我试图在两个单独的表格中检索已回答和未回答的问题。但在输出中,我在已回答问题表中显示了已回答和未回答的问题。这里出了什么问题?我感谢所有的帮助和努力。这是我的代码:-

<%@page import="model.QuestionBean"%>
<%@page import="java.util.List"%>
<%@page import="model.QuestionDAO"%>
<%@page import="model.QuestionDAOFactory"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>All Questions</title>
    </head>
    <body>
        <div>



                    <%
                    QuestionDAOFactory qdf=new QuestionDAOFactory();
                    QuestionDAO qd=qdf.createQuestionDAO();
                    List<QuestionBean> list=qd.getQuestions();
                    for (QuestionBean qb : list) {
                    %>

                        <%
                        if(qb.getIsAnswered().equalsIgnoreCase("Y"))
                        %>
                        <table style="width: 50%;height: 100%;border: 1px solid black;" align="">
                            <thead>
                                <tr>
                                    <th>Answered Questions</th>
                                </tr>
                            </thead>
                            <tbody>
                                <%
                                    {
                                %>
                            <tr><td><a href="viewQuestion.jsp?id=<%=qb.getQuestionId() %>"><%=qb.getQuestionText() %></a></td></tr>
                                <%
                                    }
                                %>
                            </tbody>
                        </table>    
                        <%
                            else
                        %>
                        <table style="width: 50%;height: 100%;border: 1px solid black;" align="">
                            <thead>
                                <tr>
                                    <th>Unanswered Questions</th>
                                </tr>
                            </thead>
                            <tbody>
                                <%
                                    {
                                %>
                            <tr><td><a href="viewQuestion.jsp?id=<%=qb.getQuestionId() %>"><%=qb.getQuestionText() %></a></td></tr>
                                <%
                                    }
                                %>
                            </tbody>
                        </table>


                    <%
                        }
                    %>

            </table>
        </div>
    </body>
</html>

【问题讨论】:

    标签: java jsp if-statement


    【解决方案1】:

    我认为问题出在您的 if-else 语句中。你的吸气剂告诉我返回值是一个布尔值,但你做了一个字符串比较:

    qb.getIsAnswered().equalsIgnoreCase("Y")
    

    更改您的数据模型或调试此行的返回值。

    请发布您的模型课程。

    【讨论】:

    • 如果qb.getIsAnswered() 返回一个布尔值,代码将无法通过编译(您不能为boolean 调用equalsIgnoreCase(或任何方法))。根据问题描述,此代码会产生一个输出(也就是说,除非 OP 看到 JSP 页面的先前编译的输出)。
    • 如果 getter 返回一个布尔值,则不需要调用 equals 并且代码更具可读性。但是上面的代码通过检查“Y”来做任何事情。我建议将值更改为布尔值并删除 equalsIgnoreCase
    【解决方案2】:

    if-else 语句的块看起来不正确。

    试试:

                        <%
                        if(qb.getIsAnswered().equalsIgnoreCase("Y")) {
                        %>
                          <table>
                            ...
                          </table>
                        <%
                        } else {
                        %> 
                          <table>
                            ...
                          </table>
                        <%
                        }
                        %>             
    

    编辑:

    仔细阅读问题后,您的JSP中的逻辑似乎是错误的。如果您希望在单独的表格中显示已回答的未回答问题,则必须首先遍历所有已回答的问题并将它们放在一个表格中,然后遍历所有未回答的问题并将它们放在第二个表格中。例如,您可以通过两个循环来实现:

    <table ...>
    ...
    <%
      for (QuestionBean qb : list) {
        if(qb.getIsAnswered().equalsIgnoreCase("Y")) {
    %>
        ... show answered question ...
    <%
        }
      }
    %>
    </table>
    <table ...>
    ...
    <%
      for (QuestionBean qb : list) {
        if(!qb.getIsAnswered().equalsIgnoreCase("Y")) {
    %>
        ... show unanswered question ...
    <%
        }
      }
    %>
    </table>
    

    或者您可以通过在 QuestionDAO 类中引入 getAnsweredQuestions()getUnAnsweredQuestions() 来避免 if 语句。

    您当前的逻辑似乎为每个问题创建了一个单独的表格。

    【讨论】:

    • @CodeHungry 你指的是我在编辑之前还是之后的回答?
    • @CodeHungry 你能说得更具体点吗?你现在看到什么输出?
    【解决方案3】:

    检查 'qb.getIsAnswered()' 的内容。可能总是'Y'

    【讨论】:

      【解决方案4】:

      有一些问题:

      • 您正在为每个 QuestionBean 创建一个新表,因为表定义在 for 循环中。
      • 您的末尾有一个不匹配的标签。

      如果您想要 2 个表,并且坚持使用上面的 API,则需要两个 for 循环来遍历返回的列表。一个循环将呈现已回答的问题,另一个循环将呈现未回答的问题。

      类似这样的:

      <%@page import="model.QuestionBean"%>
      <%@page import="java.util.List"%>
      <%@page import="model.QuestionDAO"%>
      <%@page import="model.QuestionDAOFactory"%>
      <%@page contentType="text/html" pageEncoding="UTF-8"%>
      <!DOCTYPE html>
      <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>All Questions</title>
        </head>
        <body>
          <div>
              <%
                QuestionDAOFactory qdf=new QuestionDAOFactory();
                QuestionDAO qd=qdf.createQuestionDAO();
                List<QuestionBean> list=qd.getQuestions();
              %>
              <table style="width: 50%;height: 100%;border: 1px solid black;" align="">
                <thead>
                  <tr>
                    <th>Answered Questions</th>
                  </tr>
                </thead>
                <tbody>
                  <%
                    for (QuestionBean qb : list) {
                      if(qb.getIsAnswered().equalsIgnoreCase("Y")) {
                  %>
                  <tr><td><a href="viewQuestion.jsp?id=<%=qb.getQuestionId() %>"><%=qb.getQuestionText() %></a></td></tr>
                   <%
                      }
                    }
                   %>
                </tbody>
              </table>    
              <table style="width: 50%;height: 100%;border: 1px solid black;" align="">
                <thead>
                  <tr>
                    <th>Unanswered Questions</th>
                  </tr>
                </thead>
                <tbody>
                   <%
                     for (QuestionBean qb : list) {
                       if(!qb.getIsAnswered().equalsIgnoreCase("Y")) {
                   %>
                   <tr><td><a href="viewQuestion.jsp?id=<%=qb.getQuestionId() %>"><%=qb.getQuestionText() %></a></td></tr>
                   <%
                       }
                     }
                   %>
                </tbody>
              </table>
          </div>
        </body>
      </html>
      

      【讨论】:

      • 感谢您的回答。我复制粘贴了你的代码。但它显示未回答类别中的所有答案。
      • 这只能意味着 getIsAnswered 方法总是返回不同于 'Y' 或 'y' 的东西。我建议打印值或调试以检查使用的值。
      最近更新 更多