平均方法无法正常运行

时间:2014-04-20 00:40:15

标签: java

这是我尝试从中获取某些值的平均方法。

我不确定我的代码有什么问题,但它无法正确编译。

我实际上没有得到任何输出。

我还需要知道hoe从main方法调用这个方法。谢谢!

public String averageFood()
    {
String averageStuff = "";
        double avg = 0;
        double num1 = 0;
        double num2 = 0;
        for (int i = 0; i < numGerbils; i++)
        {
            averageStuff = averageStuff + gerbilArray[i].getID();
            averageStuff = averageStuff + " (";
            averageStuff = averageStuff + gerbilArray[i].getName();
            averageStuff = averageStuff + ") ";
            for (int k = 0; k < foodnum; k++)
            {
                num1 = num1 + foodArray[k].getamtOfFood();
                num2 = num2 + gerbilArray[i].getGerbFood()[k].getamtOfFood();
            }
            avg = 100*(num2/num1);
            avg = Math.round(avg);
            averageStuff = averageStuff + Double.toString(avg);
            averageStuff = averageStuff + "%\n";
        }
        return averageStuff;
    }

公共类Gerbil {

String ID;
String name;
//private int[] consumption;
boolean bite; 
boolean escape;
Food[] gerbFood;
int sum;

//public Gerbil (int numOfFood) {

    //consumption = new int[numOfFood];
//}

public Gerbil(String ID, String name, boolean bite, boolean escape, Food[] gerbFood)
{

    this.ID = ID;
    this.name = name;
    this.bite = bite;
    this.escape = escape;
    this.gerbFood = gerbFood;

}

public String getID() {
    return ID;

}


//public void setID(String Id) {
    //Id = ID;
//}

public String getName () {
    return name;
}

public Food[] getGerbFood() {
    return gerbFood;
}

//public void setName (String name) {
    //this.name = name;
//}

//public int[] getConsumption () {
    //return consumption;
//}
public String getBite() {
    if(bite == true) {
        return "will bite";
    }
    else {
        return "will not bite";
    }

}
//public void setBite(boolean bite) {
    //this.bite = bite;
//}

public String getEscape() {
    if(escape == true) {
    return "will escape";
    }
    else {
        return "won't escape";
    }
}

公共课程食物{

String nameOfFood;
String colorOfFood;
int amtOfFood;

public Food (String nameOfFood, String colorOfFood, int amtOfFood)
{
    this.nameOfFood = nameOfFood;
    this.colorOfFood = colorOfFood;
    this.amtOfFood = amtOfFood;
}

public String getnameOfFood()
{
    return nameOfFood;
}

public String getcolorOfFood()
{
    return colorOfFood;
}

public int getamtOfFood()
    {
    return amtOfFood;
    }
}

2 个答案:

答案 0 :(得分:0)

是删除 - &gt; ``

您可以在main中调用该方法:

String someString = avrageFood();

答案 1 :(得分:0)

您没有提供足够的代码来为您提供良好的答案。

我们需要看到averageFood()所在的其他课程(我称之为“Foo&#39;”)。 如果averageFood()不属于某个班级,那么这就是您的问题之一。

  1. 您的方法本地未定义许多变量。他们是Foo的成员吗?:

    • numGerbils
    • gerbilArray
    • foodnum
    • foodArray

  2. main()static方法,但averageFood()非静态方法。因此,main()无法直接调用averageFood() - 它需要通过Foo实例访问该方法,如下所示:
  3. public static void main(String[] args) { final Foo foo = new Foo(); foo.averageFood(); }

    如果您仍有问题,请显示所有您的代码。