Java 如何将数组与构造函数结合使用?

Java 如何将数组与构造函数结合使用?,java,arrays,constructor,Java,Arrays,Constructor,我目前正在制作一个抑郁症模拟程序,我在其中创建了具有多个参数的人体对象,这些参数会改变抑郁症影响他们的方式 我的问题是,我能够创建奇异的人类对象,例如 人类史蒂夫=新人类(“史蒂夫”,25,'m',97000,1,3,假,假,1,人类。幸福) 但我不知道如何创建包含单个人拥有的参数的人工数组。例如,我可以创建一个人类数组Human[]test=newhuman[3]中有3个人,但我不知道如何为这些人提供参数 import java.util.ArrayList; public class Hu

我目前正在制作一个抑郁症模拟程序,我在其中创建了具有多个参数的人体对象,这些参数会改变抑郁症影响他们的方式

我的问题是,我能够创建奇异的人类对象,例如
人类史蒂夫=新人类(“史蒂夫”,25,'m',97000,1,3,假,假,1,人类。幸福)
但我不知道如何创建包含单个人拥有的参数的人工数组。例如,我可以创建一个人类数组
Human[]test=newhuman[3]中有3个人,但我不知道如何为这些人提供参数

import java.util.ArrayList;

public class Human {
    private String name; // your name
    private int age; // how old you are
    private int wealth; // your average yearly income
    private int amtFriendsFamily; // how many friends/family members you are
                                    // close to
    private int levelOfEducation; /*
                                     * 1 = no education, 2 = high school
                                     * education, 3 = college education, 4 =
                                     * upper level college education
                                     */
    private boolean married;
    private boolean seeksDoctor; // true = seeks a doctor, false = does not seek
                                    // a doctor
    private char sex; // m = male, f = female
    private int race; // 1 = white, 2 = black, 3 = hispanic, 4 = asian, 5 =
                        // mideast
    static int happiness = 75;

    private String sSex, sMarried, sDoct, sRace, sEdu;
    private String[] names = { "Sally", "Karl", "Steven", "Emily" };

    public Human(String name1, int age1, char sex1, int wealth1, int amtFriendsFamily1, int levelOfEducation1,
            boolean married1, boolean seeksDoctor1, int race1, int happiness1) {
        name = name1;
        age = age1;
        wealth = wealth1;
        amtFriendsFamily = amtFriendsFamily1;
        levelOfEducation = levelOfEducation1;
        married = married1;
        seeksDoctor = seeksDoctor1;
        sex = sex1;
        race = race1;
        happiness = happiness1;

        if (sex == 'm')
            sSex = "male";
        else if (sex == 'f')
            sSex = "female";
        else
            sSex = "foreign gender";

        if (married == false)
            sMarried = "is not married, ";
        else
            sMarried = "is married, ";

        if (seeksDoctor == false)
            sDoct = "does not seek doctors.";
        else
            sDoct = "seeks doctors.";

        if (race == 1)
            sRace = "White";
        else if (race == 2)
            sRace = "Black";
        else if (race == 3)
            sRace = "Hispanic";
        else if (race == 4)
            sRace = "Asian";
        else if (race == 5)
            sRace = "Middle Eastern";

        if (levelOfEducation == 1)
            sEdu = " has no formal education, ";
        else if (levelOfEducation == 2)
            sEdu = " has a high school education, ";
        else if (levelOfEducation == 3)
            sEdu = " has a college education, ";
        else if (levelOfEducation == 4)
            sEdu = " has an upper-college level or higher education, ";

        System.out.println(name + " is a " + age + " year old " + sRace + " " + sSex + " who makes $" + wealth
                + " per year," + " has " + amtFriendsFamily + " friends and family members combined," + sEdu + sMarried
                + "and " + sDoct);
    }
}

你可以这样做

// doesn't need size in initialization, the compiler can count the number of new Human instances 
Human[] test = new Human[] {
    new Human(...), new Human(...), new Human(...)
}
或者使用索引

Human[] test = new Human[3];
test[0] = new Human(...);

你可以这样做

// doesn't need size in initialization, the compiler can count the number of new Human instances 
Human[] test = new Human[] {
    new Human(...), new Human(...), new Human(...)
}
或者使用索引

Human[] test = new Human[3];
test[0] = new Human(...);

我想你要找的是以下几点:

Human[] test = new Human[3];
Human temp = new Human("Steve", 25, 'm', 97000, 1, 3, false, false, 1, Human.happiness);
test[0] = temp;

只需为数组中的每个对象重新定义
temp
,然后将数组中的一个对象设置为
temp
。当然,最好是在循环中执行此操作,但在这种情况下,只有当程序获取每个
人类对象的用户/文件输入时,才可能执行此操作。

我认为您要查找的是以下内容:

Human[] test = new Human[3];
Human temp = new Human("Steve", 25, 'm', 97000, 1, 3, false, false, 1, Human.happiness);
test[0] = temp;

只需为数组中的每个对象重新定义
temp
,然后将数组中的一个对象设置为
temp
。当然,最好是在循环中执行此操作,但在这种情况下,只有当程序为每个
人类对象
获取用户/文件输入时,才可能执行此操作。

我觉得没有想到这一点太愚蠢了。我要做的是
Human[]test=newhuman[3]
humantemp=新人类(“史蒂夫”,25,'m',97000,1,3,假,假,1,人类。幸福)
测试[0]=温度但不是设置值,而是随机化,每次都创建一个新的、独特的人,对吗?我们都有过。有时候,它只是从一个全新的角度来看;)我觉得自己太傻了,没想到这一点。我要做的是
Human[]test=newhuman[3]
humantemp=新人类(“史蒂夫”,25,'m',97000,1,3,假,假,1,人类。幸福)
测试[0]=温度但不是设置值,而是随机化,每次都创建一个新的、独特的人,对吗?我们都有过。有时候,它只是从一个全新的角度来看;)