gpt4 book ai didi

java - 对文件中的数组进行排序并打印结果

转载 作者:太空宇宙 更新时间:2023-11-04 14:59:17 25 4
gpt4 key购买 nike

我必须读取一个数据文件并将其存储到一个整数数组中,对数组进行排序,然后报告最高总数和最低总数,但由于某种原因,当我运行代码时没有出现任何内容,它说它没有错误。这是我到目前为止的代码...

import java.io.*;
import java.util.Scanner;

public class CarbonAnalysis {
public static void main (String [] Args) throws FileNotFoundException {

Scanner s = new Scanner(System.in);
File f = new File("carbon_data.txt");
Scanner welcome = new Scanner(f);
File outputFile = new File("carbon_report.txt");
PrintStream output = new PrintStream(outputFile);
String firstLine = welcome.nextLine();
int secondLine = welcome.nextInt();

CarbonDioxideData[] Country = new CarbonDioxideData[secondLine];

for(int i = 0; i < secondLine; i++) {
Country[i] = new CarbonDioxideData();
Country[i].setCountry(welcome.next());
Country[i].setTotalCO2(welcome.nextDouble());
Country[i].setRoadCO2(welcome.nextDouble());
Country[i].setCO2PerPerson(welcome.nextDouble());
Country[i].setCarsPerPerson(welcome.nextInt());
}
int count = 0;
int count2 = 0;
CarbonDioxideData[] totalEmissions = new CarbonDioxideData[count];
CarbonDioxideData[] perPersonRoadEmissions = new CarbonDioxideData[count2];

reportDescription(output);
sortTotalEmissions(totalEmissions);
sortPerPersonRoadEmissions(perPersonRoadEmissions);

}
//prints the output of data analyzed
public static void reportDescription(PrintStream output) {
output.println("Country with the lowest total emissions: ");
output.println("Country with the highest total emissions: " );
output.println("Canada is ranked for lowest total emissions.");
output.println();
output.println("Country with the lower per-person road emissions: ");
output.println("Country with the highest per-person road emissions: ");
output.println("Canada is ranked for the lowest per-road emissions.");
}
//sorts the total Emissions from highest to lowest
public static void sortTotalEmissions(CarbonDioxideData[] totalEmissions){
for(int i = 0; i < totalEmissions.length; i++) {
double max = totalEmissions[i].getTotalCO2();
int maxPos = i;
for(int j = i; j < totalEmissions.length; j++) {
if(max < totalEmissions[j].getTotalCO2() ) {
max = totalEmissions[j].getTotalCO2();
maxPos = j;
}
}
CarbonDioxideData temp = totalEmissions[maxPos];
totalEmissions[maxPos] = totalEmissions[i];
totalEmissions[i] = temp;
}
}
//sorts the per person road Emissions from highest to lowest
public static void sortPerPersonRoadEmissions(CarbonDioxideData[] perPersonRoadEmissions){
for(int i = 0; i < perPersonRoadEmissions.length; i++) {
int max = perPersonRoadEmissions[i].getCarsPerPerson();
int maxPos = i;
for(int j = i; j < perPersonRoadEmissions.length; j++) {
if(max < perPersonRoadEmissions[j].getCarsPerPerson() ) {
max = perPersonRoadEmissions[j].getCarsPerPerson();
maxPos = j;
}
}
CarbonDioxideData temp = perPersonRoadEmissions[maxPos];
perPersonRoadEmissions[maxPos] = perPersonRoadEmissions[i];
perPersonRoadEmissions[i] = temp;
}
}

}

给我提供帮助的代码:

public class CarbonDioxideData {


private String country;

private double totalCO2;

private double roadCO2;

private double CO2PerPerson;

private int carsPerPerson;

public CarbonDioxideData() {
country = "blank_country";
totalCO2 = -1.0;
roadCO2 = -1.0;
CO2PerPerson = -1.0;
carsPerPerson = -1;
}

public String toString() {
String result = country;

result += " " + totalCO2;
result += " " + roadCO2;
result += " " + CO2PerPerson;
result += " " + carsPerPerson;

return result;
}
public String getCountry() {
return country;
}

public void setCountry(String country) {
this.country = country;
}

public double getTotalCO2() {
return totalCO2;
}

public void setTotalCO2(double totalCO2) {
this.totalCO2 = totalCO2;
}

public double getRoadCO2() {
return roadCO2;
}

public void setRoadCO2(double roadCO2) {
this.roadCO2 = roadCO2;
}

public double getCO2PerPerson() {
return CO2PerPerson;
}

public void setCO2PerPerson(double cO2PerPerson) {
CO2PerPerson = cO2PerPerson;
}
public int getCarsPerPerson() {
return carsPerPerson;
}

public void setCarsPerPerson(int carsPerPerson) {
this.carsPerPerson = carsPerPerson;
}

}

最佳答案

你的程序总是在文件中输出一个常量字符串,因为reportDescription中没有变量。

其次,为了正确地对数组进行排序,CarbonDiamideData 应该实现 Comparable 。然后你可以打电话 Arrays.sort像这样:

Arrays.sort(perPersonRoadEmissions)

然后检索最高/值:

perPersonRoadEmissions[0] / perPersonRoadEmissions[perPersonRoadEmissions.length-1]

要显示信息,您必须在排序后调用输出并更改签名以接受最高和最低值的变量。

Arrays.sort(totalEmissions);
Arrays.sort(perPersonRoadEmissions)
reportDescription(output,totalEmissions[0],totalEmissions[totalEmissions.length-1],perPersonRoadEmissions[0],perPersonRoadEmissions[perPersonRoadEmissions.length-1]);

作为替代方案,您也可以简单地将数组作为参数传递,并在 reportDescription 正文中检索 min max :

 reportDescription(output,totalEmissions,perPersonRoadEmissions);

如何更改reportDescription的内容并实现compareTo留给OP。

关于java - 对文件中的数组进行排序并打印结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22804727/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com