所以我创建了一个搜索数组的方法,该数组是CO2Data类的一部分。这是方法:
public static CO2Data highest (CO2Data [] arr2){
Scanner sc = new Scanner(System.in);
CO2Data highestindex = arr2[0];
for (int i = 0; i<arr2.length; i++){
if (arr2[i].getTotalCO2() > highestindex.getTotalCO2()){
highestindex = arr2[i];
}
}
System.out.println(highestindex.getTotalCO2());
return highestindex;
}
该方法找到正确的最高值,但我希望它打印出与该值相关联的国家/地区。值来自此数据文件:
"Country" "Total CO2 2005 (million tonnes)" "Road CO2 (million tonnes)" "Road CO2 per person (tonnes)" "Cars per 1000 people"
10
USA 5951.13 1530.3 5.16 777
UK 2573.4 119.68 1.99 470
Italy 476.08 116.86 2 592
Germany 841.78 150.21 1.82 550
Canada 553.02 123.42 3.82 562
France 414.03 128.13 2.04 477
Russia 1575.44 114.69 0.8 178
Japan 1254.47 224.24 1.76 447
China 5100.6 228.02 0.3 17
India 1147.46 91.06 0.1 8
该方法打印出来的数字是5951.13,但我希望它打印出美国,因为这是与该数字相关联的国家/地区。我试图修改我的最高方法,尝试将国家/地区与之关联,但该代码无法编译。那是什么 错了吗?
public static CO2Data highest (CO2Data [] arr2){
Scanner sc = new Scanner(System.in);
CO2Data highestindex = arr2[0];
CO2Data currentcountry = arr2[0].getCountry(sc.nextLine());
for (int i = 0; i<arr2.length; i++){
if (arr2[i].getTotalCO2() > highestindex.getTotalCO2()){
highestindex = arr2[i];
currentcountry = arr2[i].getCountry(sc.nextLine());
}
}
System.out.println(highestindex.getTotalCO2());
return highestindex;
}
这是CO2Data类:
public class CO2Data {
private String country; //A private variable will prevent other users from accessng and chaning these variables.
private double totalCO2;
private double roadCO2;
private double CO2PerPerson;
private int carsPerPerson;
public CO2Data() {
country = "";//this sets the initial values for the different variables
totalCO2 = 0;
roadCO2 = 0;
CO2PerPerson = 0;
carsPerPerson = 0;
}
public String getCountry() {
return country;
}
public void setCountry(String country) { //to set the country you have to use the this command to access the private variable
this.country = country; //you have to use this.country instead of just country because the string county has been declared as a private variable.
}
public double getTotalCO2() {
return totalCO2;
}
public void setTotalCO2(double totalCO2) {
this.totalCO2 = totalCO2; //The this.item command allows you to access private variables.
}
public double getRoadCO2() {
return roadCO2;
}
public void setRoadCO2(double roadCO2) {
this.roadCO2 = roadCO2;
}
public double getCO2PerPerson() {
return CO2PerPerson;
}
public void setCO2PerPerson(double cO2PerPerson) {
this.CO2PerPerson = cO2PerPerson;
}
public int getCarsPerPerson() {
return carsPerPerson;
}
public void setCarsPerPerson(int carsPerPerson) {
this.carsPerPerson = carsPerPerson;
}
}
答案 0 :(得分:1)
提示......以修辞问题的形式出现:
哪个字段包含CO2Data
对象的国家/地区?
您如何获得该字段的价值?
如何打印?
另外......
这条线做什么......为什么(显然)错了?
CO2Data currentcountry = arr2[0].getCountry(sc.nextLine());
(查看getCountry
的声明。它的目的是什么?需要哪些论据?)
答案 1 :(得分:0)
您可以对所需列上的数组进行排序,说“Total CO2 2005”,然后选择MAX的第一个节点
答案 2 :(得分:0)
这是我修改后的代码
public static String highest (CO2Data [] arr2){
Scanner sc = new Scanner(System.in);
CO2Data highestindex = arr2[0];
for (int i = 0; i<arr2.length; i++){
if (arr2[i].getTotalCO2() > highestindex.getTotalCO2()){
highestindex = arr2[i];
}
}
return highestindex.getCountry();
}
即使我修改carbon.data,此代码也始终有效。我最近修改它,以便英国是最高价值,它打印出英国。