gpt4 book ai didi

java - 如果 Boolean 为 True,如何返回字符串

转载 作者:行者123 更新时间:2023-11-29 07:26:57 25 4
gpt4 key购买 nike

在 Student 类中,构造函数中写入了一些值。我想在 VIP 栏中打印。即当 boolean 值为 true 时是,反之亦然。

public class Student {

private String firstName;
private String lastName;
private boolean goldCustomer;

public Student(String firstName, String lastName, boolean goldCustomer) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.goldCustomer = goldCustomer;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public boolean isGoldCustomer() {
return goldCustomer;
}

public void setGoldCustomer(boolean goldCustomer) {
this.goldCustomer = goldCustomer;
}

}

我的 JSP 页面

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="java.util.* , com.jsptag.demo.Student" %>
<%
List<Student> students = new ArrayList< >();
students.add( new Student("Rahul" , "Rawat" , false));
students.add( new Student("Rohit" , "Negi" , true));
students.add( new Student("Mahesh" , "Gupta" , false));
pageContext.setAttribute("myStudents", students);
%>

<html>
<body>
<table border="1">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>VIP ?</th>
</tr>
<c:forEach var="data" items="${myStudents}">
<tr>
<td>${data.firstName}</td>
<td>${data.lastName}</td>
<td>${data.goldCustomer}</td>
</tr>
</c:forEach>
</table>
</body>
</html>

最佳答案

它应该与三元运算符一起工作:

<td> ${data.goldCustomer ? 'Yes' : 'No'} </td>

关于java - 如果 Boolean 为 True,如何返回字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50412307/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com