【问题标题】:How do I write a method to determine the highest value in an array如何编写一种方法来确定数组中的最大值
【发布时间】:2015-02-04 04:28:06
【问题描述】:

该数组是名为 CO2Data 的类的一部分,因此该数组是一个 CO2Data 数组。问题是我试图找到数组中的最大值,但我的方法不起作用。要从数组中获取值,该方法必须首先从 CO2data 类中获取它们。那么我的方法有什么问题:

    public static CO2Data highest (CO2Data []  arr2){
Scanner sc = null;
CO2Data highestindex = arr2[0].getTotalCO2;
for (int i = 0; i<arr2.length; i++){
arr2[i].getTotalCO2(sc.nextDouble());
if(arr2[i].getTotalCO2() > highestindex ) {
    highestindex = arr2[i].getTotalCO2();
}
}
System.out.println(highestindex);
return highestindex;
}

CO2data 类如下所示:

公开课 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;
    }
}

【问题讨论】:

  • 你不能在 Java 中使用原始比较运算符 &lt;, ==, &gt;, etc.. 比较 Object 类型
  • 怎么了?您尝试从Scanner 读取,即null...
  • @Kon == 当然适用于非原始类型
  • 这不会编译.. (highestindex = arr2[0];)
  • @August - 是的。但是== 在您了解内存空间之前不会以您期望的方式工作。在您正在创建的类上覆盖 equals 方法是可行的方法。

标签: java arrays class


【解决方案1】:

我能看到的三个问题。

首先,你永远不会实例化 sc(你将它设置为 null,然后什么都不做)。

其次,您正在为对象设置双精度。 (问题更新后不再有效)

第三,您正在将一个对象与一个双精度对象进行比较。

您很可能想要比较对象的属性。可能是这样的:

if( arr2[i].getTotalCO2() > highestindex.getTotalCO2() ) {
    highestindex = arr2[i];
}

--编辑-- 另外,将最高索引行更改为:

CO2Data highestindex = arr2[0];

【讨论】:

  • 它修复了大多数错误,除了我不知道将最高索引设置为哪种类型的变量并且我不确定如何将其设置为数组的值 0。
  • CO2Data 类是什么样的?您能否将其添加到问题中,或者它是否太长或包含敏感信息?
  • 好的,更新了我的答案。不过,我不确定“最高指数”的定义是什么。这是否意味着最高的二氧化碳总量,还是其他什么?
  • 最高指数是二氧化碳排放量最高的国家。我还用你的添加更新了我的代码,但它仍然没有编译。现在怎么了?
  • 现在highestindex 不再是double。我会更新我的答案以匹配你的代码。
【解决方案2】:

我确定你的代码不是工作代码。

例如,你怎么能这样做:

double highestindex = arr2[0];

或者在你的方法中你的 Scanner 类有什么意义?它不会起作用。

所以如果你把这么奇怪的代码放在这里很难帮助你。 试试这个:

public static double highest (CO2Data []  arr2){
    Scanner sc = new Scanner(System.in);
    CO2Data highestindex = arr2[0];
    for (int i = 0; i<arr2.length; i++){
    arr2[i].setTotalCO2(sc.nextDouble());
        if (arr2[i].getTotalCO2() > highestindex.getTotalCO2()){
            highestindex = arr2[i];
            }
        }
    System.out.println(highestindex.getTotalCO2());
    return highestindex.getTotalCO2();
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多