gpt4 book ai didi

java - 一个程序中有多种方法

转载 作者:行者123 更新时间:2023-12-02 06:09:59 25 4
gpt4 key购买 nike

我正在尝试将数组 (fishWeights) 设置为使用方法找到的值。除了当我尝试编译它时:

public class GoFishEdited {
public static void main(String[] args) {

System.out.println("\nProject 1, Stage 3\n");

Habitat h1 = new Habitat();
Habitat h2 = new Habitat();

int[] fishWeights = stockUp();

System.out.println("Start with some weights:");
for (int i : fishWeights) {
System.out.print(i + " ");
}

System.out.println("\n\nMake fish of those weights.\n");

Fish[] fishGroup = new Fish[fishWeights.length]; // array of Fish

for (int i=0; i < fishWeights.length; i++) {
fishGroup[i] = new Fish(fishWeights[i]); // make fish
}

}
}

它指出无法找到符号 stockUp()。它在这个文件中:

public class Habitat {

ArrayList stringer = new ArrayList();
public int maxCount=25;
public int minCount=9;
public int maxWeight=10;
public int minWeight=1;
public int catchProbability=30; //0.3

public void stockUp(int[] fishArr){

int numofF = minCount + (int)(Math.random() * ((maxCount - minCount) + 1));

for(int i = 0; i<numofF; i++){
fishArr[i] = minWeight + (int)(Math.random() * ((maxWeight - minWeight) + 1));
}
}

public Habitat(){

}

public void addFish(Fish f) {
stringer.add(f);
}

public void removeFish(Fish f){
stringer.remove(f);
}

public void printFish(){
System.out.println(stringer);
}
}

所以 stockUp 存在,我只是似乎无法让 getFishEdited 找到它。

最佳答案

在java中,一切都在对象中。因此,如果你想从类中调用方法,应该使用

Habitat habitat = new Habitat();
habitat.stockUp();

关于java - 一个程序中有多种方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21976389/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com