gpt4 book ai didi

Java:构造函数 gerbil(int) 未定义

转载 作者:行者123 更新时间:2023-12-01 13:07:27 25 4
gpt4 key购买 nike

所以我已经阅读了 stackoverflow 上所有构造函数未定义的帖子,并尝试了解决方案,但它们对我不起作用。也许我尝试错了。我不断收到“构造函数 Gerbil(int) 未定义。”

有问题的代码:

GerbilArray[i] = new Gerbil(i);

我的完整代码:

import java.util.Scanner;
public class Gerbil {

public String name;
public String id;
public String bite;
public String escape;

public Gerbil() {
this.name = "";
this.id = "";
this.bite = "";
this.escape = "";
}

public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("How many foods?");
int totalFood = keyboard.nextInt();

System.out.println("How many gerbils in the lab?");

int numberOfGerbils = keyboard.nextInt();
Gerbil[] GerbilArray = new Gerbil[numberOfGerbils];

for(int i = 0; i <= numberOfGerbils; i++){
GerbilArray[i] = new Gerbil(i);

System.out.print("Lab ID:");
String id = keyboard.next();

System.out.print("Gerbil Nickname:");
String name = keyboard.next();

System.out.print("Bite?");
String bite = keyboard.next();

System.out.print("Escapes?");
String city = keyboard.nextLine();

for (int j = 0; j < totalFood; j++) {
System.out.println("How many of food " + (j+1) + "do you eat?:");

}

}
}
}

此外,您可能已经看到我的嵌套 for 循环还没有完成。我试图在一个对象内创建一个数组,该数组将在用户列出的对象内存储“x”个整数(inttotalFood),但我不知道如何操作。

最佳答案

您在 Gerbil 类中没有构造函数 Gerbil(int a),而您尝试调用它!

就这样调用它:

GerbilArray[i] = new Gerbil();

关于Java:构造函数 gerbil(int) 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23165449/

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