编写程序newclass2.jsp,学会在声明中定义类student,初始化该类(学号、姓名、成绩),用表格形式输出“学号、姓名、成绩”
时间: 2024-05-31 12:07:58 浏览: 65
编一个程序,定义结构类型(有学号、姓名、性别和程序设计成绩四个字段),声明该结构类型变量,用赋值语句对该变量赋值以后再输出。
<%@ page language="java" %>
<%@ page import="java.util.*" %>
<%
class Student {
private String id;
private String name;
private int score;
public Student(String id, String name, int score) {
this.id = id;
this.name = name;
this.score = score;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
List<Student> students = new ArrayList<>();
students.add(new Student("1001", "张三", 90));
students.add(new Student("1002", "李四", 85));
students.add(new Student("1003", "王五", 92));
%>
<html>
<head>
<title>学生信息</title>
</head>
<body>
<table border="1">
<tr>
<th>学号</th>
<th>姓名</th>
<th>成绩</th>
</tr>
<% for (Student student : students) { %>
<tr>
<td><%= student.getId() %></td>
<td><%= student.getName() %></td>
<td><%= student.getScore() %></td>
</tr>
<% } %>
</table>
</body>
</html>
阅读全文