构造函数不读取int []

时间:2014-04-20 18:24:10

标签: 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(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应该读出foodeats [](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());
}



}

为了让沙鼠构造函数读取foodeats int [],还有什么我需要添加的吗?

我有一种感觉,因为这会导致2d数组,id必须重新定义构造函数的变量才能存储int数组

1 个答案:

答案 0 :(得分:1)

每当您打印一个数组时,您将获得其对象类型的默认字符串。

如果您想确保获取内容,请使用Arrays.toString()包中的java.util包装数组。请务必在程序顶部写import java.util.Arrays;以使用该类。

由于您在toString Gerbil的{​​{1}}中出现的唯一地方,您可以将其重写为:

public String toString() {
    return  (this.getId() + " " + this.getName() + " "
                          + Arrays.toString(this.getFoodeats())
                          + " " + this.getBite() + " "
                          + this.getEscape());
}