728x90
자바 패키지를 만들어 Pirate클래스를 만든다
package onepiece;
public class Pirate {
public String name;
public String gender; //남, 여
public boolean devilFruit; // 열매능력자 -> true
public int age;
public String team;
}
Pirate클래스로 배열을 만들고 10명의 만화 주인공들의 정보를 넣는다.
그 후 테이블을 만들고 조건에 맞게 출력!
pirateList.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import = "onepiece.Pirate"%>
<%
Pirate[] db = new Pirate[10]; // db <- 참조값, db[0] ~ db[9] null;
db[0] = new Pirate();
db[0].name = "몽키D.루피";
db[0].gender = "남";
db[0].devilFruit = true;
db[0].age = 19;
db[0].team = "밀짚모자해적단";
db[1] = new Pirate();
db[1].name = "롤로노아조로";
db[1].gender = "남";
db[1].devilFruit = false;
db[1].age = 21;
db[1].team = "밀짚모자해적단";
db[2] = new Pirate();
db[2].name = "나미";
db[2].gender = "여";
db[2].devilFruit = false;
db[2].age = 20;
db[2].team = "밀짚모자해적단";
db[3] = new Pirate();
db[3].name = "우솝";
db[3].gender = "남";
db[3].devilFruit = false;
db[3].age = 19;
db[3].team = "밀짚모자해적단";
db[4] = new Pirate();
db[4].name = "상디";
db[4].gender = "남";
db[4].devilFruit = false;
db[4].age = 20;
db[4].team = "밀짚모자해적단";
db[5] = new Pirate();
db[5].name = "토니토니초파";
db[5].gender = "남";
db[5].devilFruit = true;
db[5].age = 7;
db[5].team = "밀짚모자해적단";
db[6] = new Pirate();
db[6].name = "니코로빈";
db[6].gender = "여";
db[6].devilFruit = true;
db[6].age = 30;
db[6].team = "밀짚모자해적단";
db[7] = new Pirate();
db[7].name = "프랑키";
db[7].gender = "남";
db[7].devilFruit = false;
db[7].age = 36;
db[7].team = "밀짚모자해적단";
db[8] = new Pirate();
db[8].name = "브룩";
db[8].gender = "남";
db[8].devilFruit = true;
db[8].age = 90;
db[8].team = "밀짚모자해적단";
db[9] = new Pirate();
db[9].name = "징베";
db[9].gender = "남";
db[9].devilFruit = true;
db[9].age = 46;
db[9].team = "밀짚모자해적단";
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>pirateList</h1>
<h2>전체 리스트</h2>
<table border="1">
<tr>
<th>name</th>
<th>gender</th>
<th>devilFruit</th>
<th>age</th>
<th>team</th>
</tr>
<%
int allCount = 0;
for(Pirate p : db) {
allCount = allCount + 1;
%>
<tr>
<td><%=p.name%></td>
<td><%=p.gender%></td>
<td><%=p.devilFruit%></td>
<td><%=p.age%></td>
<td><%=p.team%></td>
</tr>
<%
}
%>
</table>
전체 : <%=allCount%>명
<hr>
<h2>남자 리스트</h2>
<table border="1">
<tr>
<th>name</th>
<th>gender</th>
<th>devilFruit</th>
<th>age</th>
<th>team</th>
</tr>
<%
int maleCount = 0;
for(Pirate p : db) {
if(p.gender.equals("남")) {
maleCount = maleCount + 1;
%>
<tr>
<td><%=p.name%></td>
<td><%=p.gender%></td>
<td><%=p.devilFruit%></td>
<td><%=p.age%></td>
<td><%=p.team%></td>
</tr>
<%
}
}
%>
</table>
남자 : <%=maleCount%>명
<hr>
<h2>열매능력자 리스트</h2>
<table border="1">
<tr>
<th>name</th>
<th>gender</th>
<th>devilFruit</th>
<th>age</th>
<th>team</th>
</tr>
<%
int devilFruitCount = 0;
for(Pirate p : db) {
if(p.devilFruit) {
devilFruitCount = devilFruitCount + 1;
%>
<tr>
<td><%=p.name%></td>
<td><%=p.gender%></td>
<td><%=p.devilFruit%></td>
<td><%=p.age%></td>
<td><%=p.team%></td>
</tr>
<%
}
}
%>
</table>
열매능력자 : <%=devilFruitCount%>명
<hr>
<h2>20대 리스트</h2>
<table border="1">
<tr>
<th>name</th>
<th>gender</th>
<th>devilFruit</th>
<th>age</th>
<th>team</th>
</tr>
<%
int age20Count = 0;
for(Pirate p : db) {
if(p.age >= 20 && p.age <= 29) {
age20Count = age20Count + 1;
%>
<tr>
<td><%=p.name%></td>
<td><%=p.gender%></td>
<td><%=p.devilFruit%></td>
<td><%=p.age%></td>
<td><%=p.team%></td>
</tr>
<%
}
}
%>
</table>
20대 : <%=age20Count%>명
<hr>
<h2>미성년자 리스트</h2>
<table border="1">
<tr>
<th>name</th>
<th>gender</th>
<th>devilFruit</th>
<th>age</th>
<th>team</th>
</tr>
<%
int ageUnder20Count = 0;
for(Pirate p : db) {
if(p.age < 20) {
ageUnder20Count = ageUnder20Count + 1;
%>
<tr>
<td><%=p.name%></td>
<td><%=p.gender%></td>
<td><%=p.devilFruit%></td>
<td><%=p.age%></td>
<td><%=p.team%></td>
</tr>
<%
}
}
%>
</table>
미성년자 : <%=ageUnder20Count%>명
<hr>
<h2>10살 ~ 30살 리스트</h2>
<table border="1">
<tr>
<th>name</th>
<th>gender</th>
<th>devilFruit</th>
<th>age</th>
<th>team</th>
</tr>
<%
int age1030Count = 0;
for(Pirate p : db) {
if(p.age >= 10 && p.age <= 30) {
age1030Count = age1030Count + 1;
%>
<tr>
<td><%=p.name%></td>
<td><%=p.gender%></td>
<td><%=p.devilFruit%></td>
<td><%=p.age%></td>
<td><%=p.team%></td>
</tr>
<%
}
}
%>
</table>
10살 ~ 30살 : <%=age1030Count%>명
<hr>
<h2>이름문자열 길이가 2인 리스트</h2>
<table border="1">
<tr>
<th>name</th>
<th>gender</th>
<th>devilFruit</th>
<th>age</th>
<th>team</th>
</tr>
<%
int nameTwoLength = 0;
for(Pirate p : db) {
if(p.name.length() == 2) {
nameTwoLength = nameTwoLength + 1;
%>
<tr>
<td><%=p.name%></td>
<td><%=p.gender%></td>
<td><%=p.devilFruit%></td>
<td><%=p.age%></td>
<td><%=p.team%></td>
</tr>
<%
}
}
%>
</table>
이름 길이 2자 : <%=nameTwoLength%>명
<hr>
<h2>이름이 "프" or "징"으로 시작하는 리스트</h2>
<table border="1">
<tr>
<th>name</th>
<th>gender</th>
<th>devilFruit</th>
<th>age</th>
<th>team</th>
</tr>
<%
int nameStartPeuOrJing = 0;
for(Pirate p : db) {
if(p.name.startsWith("프") || p.name.startsWith("징")) {
nameStartPeuOrJing = nameStartPeuOrJing + 1;
%>
<tr>
<td><%=p.name%></td>
<td><%=p.gender%></td>
<td><%=p.devilFruit%></td>
<td><%=p.age%></td>
<td><%=p.team%></td>
</tr>
<%
}
}
%>
</table>
이름이 "프" or "징"으로 시작 : <%=nameStartPeuOrJing%>명
</body>
</html>
728x90
'STUDY > JSP' 카테고리의 다른 글
20240312 <a>태그로 값 넘겨 구구단 출력 (0) | 2024.03.12 |
---|---|
20240312 카드 출력 (0) | 2024.03.12 |
20240312 로또 추첨기 (0) | 2024.03.12 |
20240308 달력(3월) (0) | 2024.03.08 |
20240308 구구단 (0) | 2024.03.08 |