Java:方法不工作 [英] Java: method not working

查看:151
本文介绍了Java:方法不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个程序,模拟一些人在湖中捕鱼,我已经创建了鱼类和池塘类,我正在工作的Fisher类和一个方法不工作,我会显示代码(我'新的编程,所以我不知道如果我提供足够的信息)

I am creating a program that simulates some people catching fish in a lake, I already created classes for Fish and Pond and I was working on the Fisher class and a method is not working and I'll show the code (I'm new to programming so I'm not sure if I am providing enough information)

public class Fisher {

  public static int LIMIT = 3;
  private String     name;
  private Fish[]     fishCaught = new Fish[LIMIT];
  private int        numFishCaught;
  private int        keepSize;

  public Fisher(String name, Fish[] fishCaught, int numFishCaught, int keepSize) {

    this.name = name;
    this.fishCaught = fishCaught;
    this.numFishCaught = numFishCaught;
    this.keepSize = keepSize;
  }

  public String getName() {
    return name;
  }

  public Fish[] getFishCaught(){
    return fishCaught;
  }

  public int getNumFishCaught() {
    return numFishCaught;

  }

  public int getKeepSize() {
    return keepSize;
  }

  public String toString() {
    return (name + " with " + numFishCaught + " fish");
  }

  public void keep(Fish f) {
    if (numFishCaught == LIMIT) {
    } else {
      numFishCaught++;
      fishCaught[numFishCaught-1] = f;
    }

  }

  boolean likes(Fish f) {
    if ((f.getSize() >= keepSize) && !(f.getSpecies().equalsIgnoreCase("Sunfish"))) {
      return true;
    }
     return false; 
  }

  public void listFish(){
    System.out.println(name + " with " + numFishCaught + " as follows: ");
    for (int i = 0; i<numFishCaught; i++){
      Fish f = new fish[i];
      System.out.println("A " + f.getSize() + " cm " + f.getFishCaught());
    }
  }

}

是listFish()方法,它应该返回如下:

the problem is the listFish() method, it's supposed to return something like this:

Bob with 2 fish as follows:
A 4 cm Pike
A 15 cm Bass

但它不工作,类型和不能找到符号错误

but it's not working it gives me "incompatible types" and "cannot find symbol" errors??

(只是为了让你的生活更容易我会包括鱼类)

(just to make your life easier i'll include the Fish class too)

public class Fish {

  private String      species;
  private int         size;


  public Fish(int size, String species) {
    this.size = size;
    this.species = species;
  }


   public String toString() {
          return " A " + size + " cm " + species;
    }

  public String getSpecies() {
    return species;
  }

  public int getSize() {
    return size;
  }


}


Error: /Users/halahalhomoud/Fisher.java:57: incompatible types
found   : Fish[]
required: Fish
File: /Users/halahalhomoud/Fisher.java  [line: 58]
Error: /Users/halahalhomoud/Fisher.java:58: cannot find symbol

编辑:
告诉你怎么样?我在Pond类中使用它,它工作正常,但我不知道为什么它不工作。

show you how? I used it in the Pond class and it worked fine but I don't get why it's not working here.

推荐答案

被渔夫捕获的鱼。现在,您可以使用 getFishCaught 检索数组中的信息。

You want the fish that are caught by a Fisher. Now, you have that information in the array you can retrieve with getFishCaught.

现在看看你尝试做什么:

Now look what you try to do instead:

 Fish f = new fish[i];
 System.out.println("A " + f.getSize() + " cm " + f.getFishCaught());

在第一行,你尝试创建一个新的数组 fish ,但它是,当然 Fish fish 解决。)。然后尝试将数组引用分配给单个 Fish f。但是鱼的数组与鱼不一样。

In the first line, you try to make a new array of fish, but it is, of course Fish (fish is the symbol that couldn't get resolved.). Then you try to assign the array reference to a single Fish f. But an array of Fish is not the same as a Fish. For example, you can eat a Fish, but not a Fish container, you know.

你可能想要的是:

 Fish f = (getFishCaught())[i];   // get the i-th Fish caught
 System.out.println("A " + f.getSize() + " cm " + f.getXXX());

其中getXXX是 Fish 的方法返回鱼类物种。 (因为你没有显示FIsh类,我不知道这个getter的确切名字)。

where getXXX is a method of Fish that returns the Fishs species. (Since you didn't show the FIsh class, I can't know the exact name of this getter).

这篇关于Java:方法不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆