因此,我正在編寫一個程序,可以查找某些人的工資的最大值,最小值,總計和平均值。我已經決定把人們的姓名列入數組列表字符串和他們的工資列入數組列表整數。我的問題是能夠輸出maxsalary,minsalary,averagesalary,我想作爲一個雙,總工資,因爲當我運行我的程序它給每個值的爲0實現和輸出arrayLists
這裏是我的代碼:
package employees;
import java.util.ArrayList;
public class Persons {
//method to add salaries
//find average and min and max
int maxSal;
int minSal;
int totalSal;
double averageSal;
public ArrayList people;
public ArrayList salaries;
public int totals() {
//int totalSal = 0;
for(int i = 0; i < salaries.size(); i++)
totalSal += salaries.indexOf(i);
return totalSal;
}
public int minSal() {
//minSal = 0;
for(int i = 0; i < salaries.size(); i++)
if(minSal > i)
minSal = i;
return minSal;
}
public int maxSal() {
//maxSal = 0;
for(int i = 0; i < salaries.size(); i++)
if(i > maxSal)
maxSal = i;
return maxSal;
}
public double averageSal() {
double averageSal =((totalSal)/5);
return averageSal;
}
}
//mainmethod
package employees;
import java.util.ArrayList;
public class PersonsTest {
Persons persons;
Job job;
public static void main(String[] args) {
Persons g = new Persons();
ArrayList<String> people = new ArrayList<String>();
people.add("Ron");
people.add("Don");
people.add("Armin");
people.add("Ferry");
people.add("Ben");
ArrayList<Integer> salaries = new ArrayList<Integer>();
salaries.add(50000);
salaries.add(40000);
salaries.add(70000);
salaries.add(55000);
salaries.add(90000);
System.out.println(g.maxSal);
System.out.println(g.minSal);
System.out.println(g.averageSal);
System.out.println(g.totalSal);
}
}
你的類包含一些瑕疵。我會建議對此做出反向處理。查看https://docs.oracle.com/javase/8/docs/api/java/util/DoubleSummaryStatistics.html(DoubleSummaryStatistics),並考慮如何重構您的類以利用此內置Java功能。 – Magnilex