java - 如何找到二维数组的总数?
问题描述
必须输入学生和科目的数量。(使用随机分数)我想打印学生的总分和科目的总分。这是下面给出的代码。
import java.util.*;
class Example{
public static void readMarks(int[][] x){
Random r=new Random();
for(int i=0; i<x.length; i++){
for(int j=0; j<x[i].length; j++){
x[i][j]=r.nextInt(101);
}
}
}
public static void printMarks(int[][] x){
for(int i=0; i<x.length; i++){
for(int j=0; j<x[i].length; j++){
System.out.print(x[i][j]+" ");
}
System.out.println();
}
}
public static int[] findStudenTot(int[][] x){
int tot[]=new int[x.length];
for(int i=0; i<x.length; i++){
for (int j = 0; j<x[i].length; j++){
tot[i]+=x[i][j];
}
}
return tot;
}
public static int[] findSubjectTot(int[][] x){
int tot1[]=new int[x.length];
for(int i=0; i<x.length; i++){
for (int j = 0; j<x[i].length; j++){
tot1[i]+=x[i][j];
}
}
return tot1;
}
public static void main(String args[]){
Scanner input=new Scanner(System.in);
System.out.print("Input no of students : ");
final int N=input.nextInt();
System.out.print("Input no of subject : ");
final int S=input.nextInt();
int[][] marks=new int[N][S];
readMarks(marks);
int[] stTotal=findStudenTot(marks);
int[] subTotal=findSubjectTot(marks);
System.out.println("total is : "+stTotal);
System.out.println("total is : "+subTotal);
}
}
如何打印 stTotal , subTotal?
如果我们假设两个输入都输入 5,那么这个输出总和是:[I@3d4eac69 ...为什么?这些字符是什么以及如何避免这些字符?
解决方案
我认为问题在于 findStudentTot 方法,您似乎返回类型为 2D 数组,但返回的是 1D 数组,所以这应该可以解决
public static int[] findStudenTot(int[][] x){
int tot[]=new int[x.length];
for(int i=0; i<x.length; i++){
for (int j = 0; j<x[i].length; j++){
tot[i]+=x[i][j];
}
}
return tot;
}
推荐阅读
- three.js - three.js:阴影中的明亮边缘
- php - 基于字符串搜索和重构解析字符串
- azure - 初始密码更改后,在 AzureAD B2C 中使用“forceChangePasswordNextLogin”创建的本地帐户无法再登录
- python - Python 通过函数调用链传递参数
- javascript - 自定义对象事件
- oracle - 在存储过程中添加异常
- css - 站点最大宽度在 Internet Explorer 中不起作用
- javascript - 从数组 onClick 渲染一个对象
- javascript - 如何在 JavaScript 中设置全局变量和全局函数
- java - 在 android studio 中创建单位转换应用程序时帮助解决问题