方法未正确显示

时间:2013-05-02 13:20:15

标签: java

以下代码中的“aboveAverage”方法无法正确显示,我已尽力尝试。有人可以解释一下出了什么问题吗?

我的代码:

import java.util.*;
public class DailyCatch
{
  private int fishermanID, fisherID;
  private String dateOfSample, date;
  private double[] fishCaught = new double[10];
  private int currWeight = 0;
  private String summary;
  private double average;
  private int aboveAvg;

  public DailyCatch() {  }

  public DailyCatch (int fishermanID, String dateOfSample)
  {
    fisherID = fishermanID;
    date = dateOfSample;
  }

  public DailyCatch (int fishermanID, String dateOfSample, String weight)
  {
    this(fishermanID, dateOfSample);
    readWeights(weight);
  }

  public void addFish(double weight)
  {
    if (currWeight > 10)
    {
       // array full
    }
    else
    {
      fishCaught[currWeight] = weight;
      currWeight += 1;  // update current index of array
    }
  }

  private void readWeights(String weightsAsString) 
  {
    String[] weightsRead = weightsAsString.split("\\s+");
    for (int i = 0; i < weightsRead.length; i++) 
    {
       this.addFish(Double.parseDouble(weightsRead[i]));
    }
  } 

  public String toString()
  {
    return "Fisherman ID: " + fisherID + "\nDate:" + date + "\nFish Caught with Weights: " + Arrays.toString(fishCaught);
  }

  public void printWeights()
  {
     for (int i = 0; i < fishCaught.length; i++)
     {
          System.out.println(fishCaught[i]);
     } 
  }

  public double averageWeight()
  {
      double sum = 0;
     double count = 0;
     for (int i = 0; i < fishCaught.length; i++)
     {
          if (fishCaught[i] != 0)
          {
                  sum += fishCaught[i];
              count += 1;
                  average = sum/count;
          }
 }
  return average;
   }

    public String getSummary()
{   int storyTellerCount = 0;
    int keeperCount = 0;
    int throwBackCount = 0;
    for (int i = 0; i < fishCaught.length; i++)
    {
        if (fishCaught[i] > 5)
        {
            storyTellerCount++;
        }

        else if (fishCaught[i] >=1 && fishCaught[i] <= 5)
        {
            keeperCount++;
        }

        else if (fishCaught[i] < 1 && fishCaught[i] > 0)
        {
            throwBackCount++;
        }

    }  String summary = ("\nStoryteller - " + storyTellerCount+ "\nKeeper - " + keeperCount + "\nThrowback - " + throwBackCount);

        return summary;
}

public int aboveAverage()
{   
    int greatAvgCount = 0;
    for (int i = 0; i < fishCaught.length; i++)
    {
        if (fishCaught[i] > average)
        {
            aboveAvg = greatAvgCount++;
        }
    }
    return aboveAvg;
}   

}

测试代码:

public class BigBass
{
public static void main (String[]args)
{
//Part 1
DailyCatch monday1 = new DailyCatch(32, "4/1/2013", "4.1 5.5 2.3 0.5 4.8 1.5");
System.out.println(monday1);

//Part 2
DailyCatch monday2 = new DailyCatch(44, "4/1/2013");
System.out.println(monday2);
monday2.addFish(2.1);
monday2.addFish(4.2);
System.out.println(monday2);

//Part 3
System.out.println("\n\nSUMMARY OF FISHERMAN 32");
System.out.println(monday1.getSummary());

//Part 4
double avg = monday1.averageWeight();
System.out.printf("\nThere are %d fish above the average weight of %.1f.", monday1.aboveAverage(), avg);
}
}

我只需要让第4部分在这里工作。它的作用是返回当我知道它应该是3时,有2条捕获的鱼高于平均值。平均值为3.1。

4 个答案:

答案 0 :(得分:3)

一个简单的错误。

public int aboveAverage() {   
    int greatAvgCount = 0;
    for (int i = 0; i < fishCaught.length; i++) {
        if (fishCaught[i] > 3.1) {
            greatAvgCount++; // no 'return'
        }
    }
    return greatAvgCount;
} 

答案 1 :(得分:0)

if (fishCaught[i] > 3.1)
        {
            return greatAvgCount++;
        }

First try : 4.1 > 3.1

returns 0 ++基本上是

您可以在循环内增加计数器,并将return语句保留为结尾。

答案 2 :(得分:0)

这一行是你的问题,

return greatAvgCount++;

你正在推行greatAvgCount然后返回它的初始值,这一行应该没有“返回”

上述平均方法应为

public int aboveAverage()
{   
    int greatAvgCount = 0;
    for (int i = 0; i < fishCaught.length; i++)
    {
        if (fishCaught[i] > 3.1)
        {
            greatAvgCount++;
        }
    }
    return greatAvgCount;
} 

另外,你可能只是为调试而做,在这种情况下足够公平,但硬编码“平均”为3.1通常被认为是不好的做法。如果你想平均值总是3.1(即它是你从书中查找的全局平均值,那么更常见的是声明一个名为double AVERAGE=3.1的静态变量,然后在需要平均值的地方使用它,那样如果“账面价值”发生变化,您只需要在代码中的某个位置更改平均值。如果根据您的数据计算平均值,您应该使用计算值。

也与你的问题没有直接关系,但为什么你使用一个数组来捕获鱼,预定义的最大值为10.如果你使用了ArrayList,你可以按照你认为合适的方式添加它,它会自动扩展以容纳< / p>

private double[] fishCaught = new double[10];

成为

private ArrayList<Double> fishCaught = new ArrayList<Double>();

答案 3 :(得分:0)

尝试

public int aboveAverage() {   
    int greatAvgCount = 0;
    for (int i = 0; i < fishCaught.length; i++) {
        if (fishCaught[i] > 3.1) {
            greatAvgCount++;
        }
    }
    return greatAvgCount;
}