补全以下代码 <script> let stu3 = {}; stu3.name = "李四"; stu3.course = []; stu3.addCourse = function (courseId, courseName) { //为当前对象的courses属性(数组)添加元素,每个元素是一个对象(属性名为课程号,属性值为课程名) //数组.push(元素) }; addCourse(stu, "001", "高等数学"); addCourse(stu, "002", "Java"); addCourse(stu, "003", "JS")
时间: 2023-06-09 20:04:30 浏览: 36
<script>
let stu3 = {};
stu3.name = "李四";
stu3.courses = [];
stu3.addCourse = function(courseId, courseName) {
let courseObj = {};
courseObj[courseId] = courseName;
this.courses.push(courseObj);
};
stu3.addCourse("001", "高等数学");
stu3.addCourse("002", "Java");
stu3.addCourse("003", "JS");
</script>
相关问题
帮我看看下面的代码为啥有问题,并纠正:<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>学生信息</title> <script type="text/javascript"> function studentDelete(studentId) { if(confirm("你确定要删除这个学生信息吗?")){ windows.location="deletestudent?userId="+studentId; } } </script> </head> <body> <table border=1> <tr> <th>学号</th> <th>姓名</th> <th>性别</th> <th>出生年龄</th> <th>入学时间</th> <th>学院</th> <th>操作</th> </tr> <c:forEach items="${stuList} " var="stu"> <tr> <td>${stu.userId}</td> <td>${stu.userName}</td> <td>${stu.sex}</td> <td>${stu.birthYear}</td> <td>${stu.grad}</td> <td>${stu.collegeName}</td> <td> <button type="button" onclick="Location='AdminServlet?action=edit$userId=${stu.userId}'">修改</button> <button type="button" onclick="studentDeLete(${stu.userId})">删除</button> </td> </tr> </c:forEach> </table> </body> </html>
这段代码有几个问题需要修正:
1. 在 `<script>` 标签中,`windows.location` 应该是 `window.location`,即将 `windows` 改为 `window`。
2. 在 `<c:forEach>` 标签中,`${stuList}` 后面有一个多余的空格,需要删除。
3. 在 `<button>` 标签中,`onclick` 属性中的 `Location` 应该是小写的 `location`,即将 `Location` 改为 `location`。
4. 在第二个 `<button>` 标签中,`onclick` 属性中的 `studentDeLete` 应该是 `studentDelete`,即将 `studentDeLete` 改为 `studentDelete`。
修正后的代码如下所示:
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学生信息</title>
<script type="text/javascript">
function studentDelete(studentId) {
if(confirm("你确定要删除这个学生信息吗?")){
window.location="deletestudent?userId="+studentId;
}
}
</script>
</head>
<body>
<table border=1>
<tr>
<th>学号</th>
<th>姓名</th>
<th>性别</th>
<th>出生年龄</th>
<th>入学时间</th>
<th>学院</th>
<th>操作</th>
</tr>
<c:forEach items="${stuList}" var="stu">
<tr>
<td>${stu.userId}</td>
<td>${stu.userName}</td>
<td>${stu.sex}</td>
<td>${stu.birthYear}</td>
<td>${stu.grad}</td>
<td>${stu.collegeName}</td>
<td>
<button type="button" onclick="location='AdminServlet?action=edit$userId=${stu.userId}'">修改</button>
<button type="button" onclick="studentDelete(${stu.userId})">删除</button>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
```
请注意检查其他部分的代码,以确保没有其他错误。
ArrayList<Student> stu=new ArrayList<>()添加对象
可以使用以下代码向 ArrayList<Student> 对象中添加一个 Student 对象:
```
Student s = new Student("John", 20); // 创建一个 Student 对象
stu.add(s); // 将该对象添加到 ArrayList<Student> 中
```
其中,Student 是一个自定义的类,代码示例中的第一行创建了一个名为 s 的 Student 对象,并指定了其姓名和年龄,第二行将该对象添加到了名为 stu 的 ArrayList<Student> 对象中。如果需要添加多个 Student 对象,可以重复以上代码多次,每次创建一个新的 Student 对象并将其添加到 ArrayList<Student> 中即可。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)