用于以下代码
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]);
}
}
我得到以下输出:
How many gerbils are in the lab?
1
0start
What is the id number of gerbil 1
123
1230
5
123
What is the name for gerbil 123
al
how much a does al eat
2
2
how much b does al eat
2
2
Does al bite? Please enter True or False
true
Does al escape? Please enter True or False
true
0
123 al [I@d07e4bc true true
2
2
本质上,[I@d07e4bc
应该读出食物中的项目[](2,2)。
下面是我为包含int[]的类Gerbil编写的代码
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());
}
}
为了让gerbil构造函数读取fooeats int[],我还需要添加什么吗?
我有一种感觉,由于这将导致一个2d数组,id必须为构造函数重新定义变量,以便存储int数组
无论何时打印数组,都将获得其对象类型的默认字符串。
如果您想确保您得到了内容,请使用java.util
包中的Arrays.toString()
包装您的数组。确保在程序的顶部编写import java.util.Arrays;
以使用该类。
因为你唯一要做的地方似乎是在Gerbil
的toString
中,所以你可以把它重写为:
public String toString() {
return (this.getId() + " " + this.getName() + " "
+ Arrays.toString(this.getFoodeats())
+ " " + this.getBite() + " "
+ this.getEscape());
}