Java 构造函数未读取int[]
对于以下代码Java 构造函数未读取int[],java,eclipse,constructor,multidimensional-array,Java,Eclipse,Constructor,Multidimensional Array,对于以下代码 System.out.println("How many gerbils are in the lab?"); int population = keyboard.nextInt(); Gerbil[] gerbil = new Gerbil[population]; gerbil = new Gerbil[population]; int b; for (b = 0; b < population; b++) {
System.out.println("How many gerbils are in the lab?");
int population = keyboard.nextInt();
Gerbil[] gerbil = new Gerbil[population];
gerbil = new Gerbil[population];
int b;
for (b = 0; b < population; b++) {
System.out.println(b + "start");
System.out.println("What is the id number of gerbil " + (b + 1));
String idnumberx = keyboard.next();
if (b == 0) {
System.out.println(idnumberx + "0");
} else {
for (int c = 0; c < gerbil.length; c++) {
if (idnumberx.equals(gerbil[c].getId())) {
System.out.println("Repeat, try again");
System.out.println(idnumberx);
b--;
System.out.println("1");
} else {
System.out.println("2");
break;
}
System.out.println("3");
}
System.out.println("4");
}
System.out.println("5");
System.out.println(idnumberx);
System.out.println("What is the name for gerbil " + idnumberx);
String nicknamex = keyboard.next();
foodeats = new int[F];
for (int e = 0; e < foodeats.length; e++) {
System.out.println("how much " + food[e].foodname + " does " + nicknamex + " eat");
int gerbileats = keyboard.nextInt();
foodeats[e] = gerbileats;
//if (gerbileats > maximum){
// {
//System.out.println("You stoopid, try again");
//e--;
//else
// {
// }
// }
// }
System.out.println(foodeats[e]);
}
for (int d = 0; d < population; d++) {
System.out.println("Does " + nicknamex + " bite? Please enter True or False");
String doesitbite = keyboard.next();
if (doesitbite.equalsIgnoreCase("true")) {
bite = Boolean.parseBoolean(doesitbite);
break;
} else if (doesitbite.equalsIgnoreCase("false")) {
bite = Boolean.parseBoolean(doesitbite);
break;
} else
System.out.println("Repeat, try again");
d--;
}
for(int d = 0; d < population; d++) {
System.out.println("Does " + nicknamex + " escape? Please enter True or False");
String doesitescape = keyboard.next();
if (doesitescape.equalsIgnoreCase("true")) {
escape = Boolean.parseBoolean(doesitescape);
break;
} else if (doesitescape.equalsIgnoreCase("false")) {
escape = Boolean.parseBoolean(doesitescape);
break;
} else
System.out.println("Repeat, try again");
d--;
}
gerbil[b] = new Gerbil(idnumberx, nicknamex, foodeats, bite, escape);
System.out.println(b);
System.out.println(gerbil[b]);
for (int k = 0; k < F; k++) {
System.out.println(foodeats[k]);
}
}
本质上,[I@d07e4bc
应该读取foodeats[](2,2)中的项目
下面是包含int[]的类沙鼠的代码
package assignment4;
import java.util.Arrays;
public class Gerbil {
public int[] foodeats;
public String idnumberx;
public String nicknamex;
public String gerbilsearch;
public boolean bite;
public boolean escape;
public String foodname;
public String searchgerbil;
public int gerbileats;
public Gerbil(String idnumberx, String nicknamex, int[] foodeats, boolean bite, boolean escape) {
this.idnumberx = idnumberx;
this.nicknamex = nicknamex;
this.escape = escape;
this.bite = bite;
this.foodeats = foodeats;
}
public Gerbil(int gerbileats){
this.gerbileats = gerbileats;
}
public boolean getBite() {
return bite;
}
public boolean getEscape() {
return escape;
}
public String getId() {
return idnumberx;
}
public String getName() {
return nicknamex;
}
public void setId(String[] newId) {
idnumberx = this.idnumberx;
}
public void setName(String[] newName) {
nicknamex = this.nicknamex;
}
public int[] getFoodeats() {
return foodeats;
}
public String toString() {
return (this.getId() + " " + this.getName() + " " + this.getFoodeats() + " " + this.getBite() + " " + this.getEscape());
}
}
为了让沙鼠构造函数读取foodeats int[],我还需要添加什么吗
我有一种感觉,因为这将产生一个2d数组,所以我必须重新定义构造函数的变量,以便在打印数组时存储int数组,您将获得其对象类型的默认字符串 如果要确保获取内容,请使用
java.util
包中的Arrays.toString()
包装数组。确保在程序顶部编写import java.util.Arrays;
以使用该类
由于您正在做的唯一地方似乎是沙土鼠的toString
,因此您可以将其重写为:
public String toString() {
return (this.getId() + " " + this.getName() + " "
+ Arrays.toString(this.getFoodeats())
+ " " + this.getBite() + " "
+ this.getEscape());
}
请把相关的代码放在你的问题上,而不是所有的问题。那肯定是很多代码
public String toString() {
return (this.getId() + " " + this.getName() + " "
+ Arrays.toString(this.getFoodeats())
+ " " + this.getBite() + " "
+ this.getEscape());
}