java用三个二维数写年级系统
时间: 2024-05-25 07:04:19 浏览: 9
好的,我可以帮您解答这个问题。您可以使用三个二维数组分别存储学生的姓名、年龄和分数,可以这样定义:
String[][] names = new String[10][30]; // 假设有10个班,每个班有30个学生
int[][] ages = new int[10][30];
double[][] scores = new double[10][30];
您可以使用 for 循环来赋值和输出数据,具体代码可以参考下面的示例:
// 赋值
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 30; j++) {
names[i][j] = "Student" + i + j;
ages[i][j] = (int) (Math.random() * 10 + 15); // 年龄在 15 到 25 岁之间
scores[i][j] = Math.random() * 100; // 分数在 0 到 100 之间
}
}
// 输出
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 30; j++) {
System.out.println("姓名:" + names[i][j] + ",年龄:" + ages[i][j] + ",分数:" + scores[i][j]);
}
}
希望能帮到您!如果您有其他问题可以直接问我哦!
相关问题
用JAVA写一个二维数组相乘任务的代码
好的,下面是使用JAVA编写二维数组相乘任务的代码:
```java
public class MatrixMultiplication {
public static void main(String[] args) {
int[][] matrixA = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
int[][] matrixB = { {9, 8, 7}, {6, 5, 4}, {3, 2, 1} };
int[][] result = multiply(matrixA, matrixB);
for(int i=0; i<result.length; i++) {
for(int j=0; j<result[0].length; j++) {
System.out.print(result[i][j] + " ");
}
System.out.println();
}
}
public static int[][] multiply(int[][] matrixA, int[][] matrixB) {
int[][] result = new int[matrixA.length][matrixB[0].length];
for(int i=0; i<matrixA.length; i++) {
for(int j=0; j<matrixB[0].length; j++) {
for(int k=0; k<matrixB.length; k++) {
result[i][j] += matrixA[i][k] * matrixB[k][j];
}
}
}
return result;
}
}
```
以上代码定义了一个`MatrixMultiplication`类,其中包含`main`方法和`multiply`方法。在`main`方法中,我们定义了两个二维数组`matrixA`和`matrixB`,然后调用`multiply`方法获取相乘的结果。最后,我们遍历打印出了结果。
`multiply`方法中通过三重循环,将两个二维数组相乘,并将结果存储在一个新的二维数组中。
希望这个代码可以帮到你。
用Java写一个可以以等腰三角形输出的二维数组
以下是可以输出等腰三角形的 Java 代码:
```
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入等腰三角形的高度:");
int height = scanner.nextInt();
int[][] triangle = new int[height][2 * height - 1];
for (int i = 0; i < height; i++) {
for (int j = 0; j < 2 * height - 1; j++) {
if (j >= height - 1 - i && j <= height - 1 + i) {
triangle[i][j] = 1;
}
}
}
for (int i = 0; i < height; i++) {
for (int j = 0; j < 2 * height - 1; j++) {
if (triangle[i][j] == 1) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
```
输入等腰三角形的高度后,程序会生成一个二维数组,然后按照等腰三角形的形状输出。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)