到目前为止,我所做的一切都是正确的,但是一旦我得到我最后的打印声明,我会得到一串字符作为我的答案,当我应该得到整个阵列的所有工资的总和。请帮忙 ! 我的输出是 第1行的人员总金额为118943 所有人的总薪水是[[I @ 1540e19d
public class SummerStats
{
private int [][] salaries;
public SummerStats(int numPeople, int numYears)
{
//initialize array
salaries = new int[numPeople][numYears];
java.util.Random r = new java.util.Random();
//fill array
for(int i=0; i<numPeople; i++)
{
for(int j=0; j<numYears; j++)
{
salaries[i][j] = r.nextInt(100000);
}
}
}
public int [][] getSalaries()
{
return salaries;
}
public int getMostMoneyOverYears()
{
int [] tmp = new int[salaries[0].length];
int k = 0;
for(int i=0; i<salaries.length; i++)
{
for(int j=0; j<salaries[i].length; j++)
{
tmp[k] += salaries[i][j];
}
k++;
}
return getMax(tmp);
}
private int getMax(int [] arry)
{
int sal1=0;
int idx=-1;
for(int i=0; i<arry.length; i++)
{
if(sal1 <arry[i])
{
sal1= arry[i];
idx = i;
}
}
return idx;
}
public int getHighestSalary()
{
//find the highest salary in a single year and return of the index
int [][] tmp = new int [salaries.length][2]; //stores salary (row) and the index (col) from the salaries [][]
int max = 0;
int idxtmp = -1;
int p=0;
for(int i=0; i<salaries.length; i++)
{
for(int j=0; j<salaries[i].length; j++)
{
if(max < salaries[i][j])
{
max = salaries[i][j];
idxtmp = j;
}
}
//add max value and its corresponding index to tmp
tmp[p][0] = max;
tmp[p][1] = idxtmp;
p++;
}
//now that we have the max for each person (row) compare salaries in each row
//reset values
max = 0;
idxtmp = -1;
for(int i=0; i<tmp.length; i++)
{
if(max < tmp[i][0])
{
max = tmp[i][0]; //this is the max salary
idxtmp = tmp[i][1]; //this is the index of the salary from the salaries[][]
}
}
return idxtmp;
}
public int getTotalSalary (int person)
{
int totalSalary = 0;
for(int i=0; i<salaries[person].length; i++)
{
totalSalary+=salaries[person][i];
}
return totalSalary;
}
//get total of all salaries
public int getAllSalaries()
{
int sum = 0;
for(int i=0; i<salaries.length; i++)
{
for(int j=0; j < salaries[i].length; j++)
{
sum += salaries[i][j];
}
}
return sum;
}
}
import java.util.Scanner;
public class TestSummerStats
{
public static void main(String [] args)
{
SummerStats ssObj = new SummerStats(2, 3);
int [][] sal = ssObj.getSalaries();
for(int i=0; i<2; i++)
{
for(int j=0; j<3; j++)
{
System.out.println("Salary at " + i + " " + j + " is $" + sal[i][j]);
}
}
//keep my output looking clean
System.out.println();
System.out.println("Index of the big baller: " + ssObj.getMostMoneyOverYears());
//keep my output looking clean
System.out.println();
System.out.println("Index of the year with the highest salary: " + ssObj.getHighestSalary());
//keep my output looking clean
System.out.println();
//promt the user to enter the person that you want the salary for
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the person you want to know the salary for.");
int person = scan.nextInt();
//keep my code clean
System.out.println();
//print out the amount of money made by the specified person
System.out.println("Total money made by person at row " + person + " is " + ssObj.getTotalSalary(person));
//sum the entire salaries
System.out.println("The total salaries of all individuals is " + ssObj.getSalaries());
}
}
答案 0 :(得分:1)
方法getSalaries()
因此被声明和定义:
public int [][] getSalaries() {
return salaries;
}
它被定义为返回一个二维数组,你看到的结果是从这样的野兽返回的默认toString()
。要获得一个实际数字,你的方法应该返回一个数字,可能是你的选择,或者是你的选择,并且在方法体内,你应该迭代你的二维数组来总结所有的工资。
答案 1 :(得分:1)
从最后一行的文字判断,您应该在最后一行中将方法调用从ssObj.getSalaries()
更改为ssObj.getAllSalaries()
。
您获得的“字符串”是将数组转换为String
时所发生的情况(通过与另一个字符串连接或通过调用数组上的toString()
显式隐式)。 / p>
答案 2 :(得分:0)
您正在调用返回数组的函数getSalaries()。你应该调用ssObj.getAllSalaries()。
您获得的字符串可能是对工资数组对象的引用。