使用Java代码块写一个1-5数字平方,可以在网页中显示的表格
时间: 2023-05-23 16:04:25 浏览: 99
A:
```java
<table>
<tr>
<th>Number</th>
<th>Square</th>
</tr>
<% for (int i = 1; i <= 5; i++) { %>
<tr>
<td><%= i %></td>
<td><%= i * i %></td>
</tr>
<% } %>
</table>
```
注意,这是一个使用Java语言的JSP代码段,需要在服务器端运行才能正确显示。如果您想要将其嵌入到HTML中,可以在JSP页面中使用`<jsp:include>`标记。
相关问题
使用Java代码块写一个1-5数字平方的表格
Sure, here's an example Java code block that generates a table of squares for integers 1 to 5:
```
public class SquareTable {
public static void main(String[] args) {
System.out.println("Number\tSquare");
for (int i = 1; i <= 5; i++) {
int square = i * i;
System.out.println(i + "\t" + square);
}
}
}
```
This code block defines a `SquareTable` class with a `main` method that prints a header for the table with the column names "Number" and "Square". It then uses a `for` loop to iterate through integers 1 to 5 and calculate the corresponding square value (which it stores in the `square` variable). Finally, it prints each number and its corresponding square value, separated by a tab character, using the `System.out.println` method. The output of this code block should look something like this:
```
Number Square
1 1
2 4
3 9
4 16
5 25
```
阅读全文