java - 构造函数不读取 int[]

标签 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[],我还需要添加什么吗?

我有一种感觉,因为这会产生一个二维数组,所以 id 必须重新定义构造函数的变量才能存储 int 数组

最佳答案

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

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

由于您所做的唯一地方似乎是在 GerbiltoString 中,因此您可以将其重写为:

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

关于java - 构造函数不读取 int[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23186067/

相关文章:

Scala 构造函数弃用

java - 如何使数组大小可变?

java - Android 文件操作 (APPEND)

eclipse - 断言不匹配 : -ea was not specified but -Dtests.asserts=true

java - (Tomcat 7) 部署的 jar 找不到文件

java - 关于Java中的构造函数

使用 vector 的 C++ 类初始化

java - 为 putLock 和 count 制作本地副本的目的是什么?

java - 进行 REST 调用(POST、GET)的 Gradle 脚本

java - Tomcat 7 - 连接重置