Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 数组元素在添加其他元素时会相互覆盖_Java_Arrays_Duplicates - Fatal编程技术网

Java 数组元素在添加其他元素时会相互覆盖

Java 数组元素在添加其他元素时会相互覆盖,java,arrays,duplicates,Java,Arrays,Duplicates,我想用Java制作一个宠物商店游戏,在这个游戏中你可以创建多只狗。当您输入“doginfo”时,程序将显示您的狗的所有信息。(这就是“目标”) 我试图制作一个包含1000个哺乳动物元素的数组,每次创建一只狗时,都会添加一个对应于该狗信息的元素 宠物店类零件: public static int count = 0; if(cmd.equalsIgnoreCase("dogInfo")) { Mammal.dogInfo(); } private static void createD

我想用Java制作一个宠物商店游戏,在这个游戏中你可以创建多只狗。当您输入“doginfo”时,程序将显示您的狗的所有信息。(这就是“目标”)

我试图制作一个包含1000个哺乳动物元素的数组,每次创建一只狗时,都会添加一个对应于该狗信息的元素

宠物店
类零件

public static int count = 0;

if(cmd.equalsIgnoreCase("dogInfo"))
{
    Mammal.dogInfo();
}

private static void createDog() 
{       

    System.out.print("Select the name of your dog: ");
    String name = scan.nextLine();
    System.out.print("Select the colour of your dog: ");
    String colour = scan.nextLine();
    System.out.print("Select the maximum height of your dog (cm): ");
    double maxHeight = scan.nextDouble();
    System.out.print("Select the gender of your dog (0 - female, 1 - male): ");
    int intGender = scan.nextInt();
    boolean male;

    if(intGender == 0)
    {
        male = false;
        err = false;
    }
    else if(intGender == 1)
    {
        male = true;
        err = false;
    }
    else
    {
        male = false;
        err = true;
    }

    if (err == false)
    {
        Mammal.mArray[count] = new Mammal(name, colour, maxHeight, male);
        count++;        
        System.out.println("A new dog was created!");
    }
    else
    {
        System.out.println("Select between 0 and 1!");
        return;
    }
}


类哺乳动物的部分

public static Mammal[] mArray = new Mammal[1000];

private static String name,colour;
private static double height,maxHeight;
private static int health;
private static boolean male;

//constructor
public Mammal(String n, String c, double mH, boolean m)
{
    name = n;
    colour = c;
    height = 9;
    maxHeight = mH;
    health = 100;
    male = m;
}

@Override
public String toString()
{
    return String.format("Name: %s, Colour: %s, Age: %d, Health: %d, Height: %.2f, Maximum Height: %.2f, Gender: %s"
                ,name, colour, age, health, height, maxHeight, male ? "Male" : "Female"
        );
}

public static void dogInfo()
{
    for (int i = 0; i < mArray.length; i++) {
        if (mArray[i] != null)
        {
            System.out.println(mArray[i]);
        }
    }
}
但实际产出是

Name: danielle, Colour: white, Age: 0, Health: 100, Height: 9,00, Maximum Height: 45,00, Gender: Female
Name: danielle, Colour: white, Age: 0, Health: 100, Height: 9,00, Maximum Height: 45,00, Gender: Female

有什么问题吗?

当将变量声明为时,更改它将使其在类的每个实例中都发生更改

为了避免这种行为,不要将它们声明为静态变量,并引用声明为静态的变量以外的变量

this.variableName
而不是

variableName

因此,您的程序可以访问调用函数的对象变量,而不是其他全局变量。

您在哪里声明了
Scanner
类?请找出原因,因为您的代码不清楚如何工作。提示:不要将类X数组放入类X中。哺乳动物类代表一种哺乳动物,因此,将一系列哺乳动物归入这一类是没有意义的。鸡蛋里没有装鸡蛋的盒子!非常感谢。我来试试。怎么样?如果你是瑞士人,正如我在你的个人资料中看到的,
Danke schön und gute Nacht
variableName