0

以下代码中的“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

4 回答 4

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;
} 
于 2013-05-02T13:24:16.087 回答
0
if (fishCaught[i] > 3.1)
        {
            return greatAvgCount++;
        }

First try : 4.1 > 3.1

true returns 0 ++基本上是 0

您可以在循环内递增计数器,并将 return 语句保留到最后。

于 2013-05-02T13:25:17.920 回答
0

这条线是你的问题,

return greatAvgCount++;

您正在增加 greatAvgCount 然后返回其初始值,此行不应有“返回”

上面的Average方法应该是

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,您可以根据需要添加它,它会自动扩展以适应

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

变成

private ArrayList<Double> fishCaught = new ArrayList<Double>();
于 2013-05-02T13:25:20.907 回答
0

尝试

public int aboveAverage() {   
    int greatAvgCount = 0;
    for (int i = 0; i < fishCaught.length; i++) {
        if (fishCaught[i] > 3.1) {
            greatAvgCount++;
        }
    }
    return greatAvgCount;
} 
于 2013-05-02T13:25:36.230 回答