我目前正在使用此选项3:下载统计信息来处理C语言如果用户选择此选项,程序将使用以下数据创建统计文件:a. Power output sorted from lowest to highest
b. Day with highest output
c. Total by month
d. Average power output for all the data统计信息文件将与输入文件具有相同的名称,但附加了_stats.txt。例如,如果输入文件名为data.txt,则stats文件将命名为data_stats.txt。我的数据文件包含January 10 2018 236.9January 11 2018 267.6January 12 2018 278.1January 13 2018 246.9January 14 2018 262.3January 15 2018 288.6February
2 2018 199.7February 3 2018 134.6February 4 2018 200.8February 5 2018 198.2March 9 2018 169.7March 10 2018 168.3March 11 2018 179.4当我运行C时,我的总功率是288.6,我如何获得每个月的总和? public static void createStatsFile(ArrayList<Entry> uploadResults) {
///////////////
///////A///////
///////////////
try {
PrintWriter writer = new PrintWriter("data_stat.txt", "UTF-8");
ArrayList<Entry> lowestToHighest = bubbleSort(uploadResults);
for(Entry entry : lowestToHighest) {
writer.print(
"Month: " + entry.getMonth() + " Day: " + entry.getDay() + " Year: " + entry.getYear() + " Power Output: "
+ entry.getPowerOutput());
writer.print("\n");
entry.print();
}我的课:public class Entry {
//declaring variables
private String month;
private String day;
private String year;
private float powerOutput;
private String file;
private int[] MonthList;
String inputFile = file;
//Constructors
public Entry() {
}
public Entry(String m, String d, String y, float p) {
month = m;
day = d;
year = y;
powerOutput = p;
}
//creating print to call ArrayList in main
public void print() {
System.out.println("Month: " + month + " Day: " + day + " Year: " + year + " Power Output: " + powerOutput);
}
public static void getFile() {
}
public String getMonth() {
return month;
}
2 回答
慕村9548890
TA贡献1884条经验 获得超4个赞
您可以使用流来获取所需结果的Map。
Map<String, Double> collect = uploadResults.stream().collect( Collectors.groupingBy(Entry::getMonth, Collectors.summingDouble(Entry::getPowerOutput)));
工作
List<Entry> uploadResults = Arrays.asList( new Entry("January","10","2018",236.9f), new Entry("January","11","2018",267.6f), new Entry("January","12","2018",278.1f), new Entry("January","13","2018",246.9f), new Entry("January","14","2018",262.3f), new Entry("January","15","2018",288.6f), new Entry("February","2","2018",199.7f), new Entry("February","3","2018",134.6f), new Entry("February","4","2018",200.8f), new Entry("February","5","2018",198.2f), new Entry("March","9","2018",169.7f), new Entry("March","10","2018",168.3f), new Entry("March","11","2018",179.4f));Map<String, Double> collect = uploadResults.stream().collect( Collectors.groupingBy(Entry::getMonth, Collectors.summingDouble(Entry::getPowerOutput)));System.out.println(collect);
产量
{March=517.3999938964844, February=733.3000030517578, January=1580.3999938964844}
编辑:
对于Java8以下的版本,应该这样做
Map<String, Float> monthMap = new HashMap<>();for (Entry uploadResult : uploadResults) { String month = uploadResult.getMonth(); Float powerOutput = uploadResult.getPowerOutput(); if( monthMap.containsKey(month) ){ powerOutput += monthMap.get(month); } monthMap.put( month, powerOutput);}System.out.println(monthMap);
繁星点点滴滴
TA贡献1803条经验 获得超3个赞
在这里,您可以在每个循环中初始化总和。
Entry tempEntry = null; for(Entry entry: uploadResults) { float sum; sum =entry.getPowerOutput(); }
float sum;
在总和之前移动并添加到总和:
Entry tempEntry = null; float sum = 0; for(Entry entry: uploadResults) { sum += entry.getPowerOutput(); }
添加回答
举报
0/150
提交
取消