使用循环添加具有随机变量的对象

时间:2015-12-27 20:10:35

标签: java

我正在尝试编写一个程序,我向用户询问他想在这个世界上实现多少人。之后,我想要与用户回答的人物对象一样多。我使用包含所有人变量(+ getters / setters)的person构造函数定义了一个person类。在此之后,我尝试创建一个循环来为我的变量赋值(大多数都是随机的)。目前,我将要创建的实例数设置为20(任意)。

This is my person class 


public class Person implements Item {
    public static final int MAX_AGE = 70;
    public static final int MAX_SEX_APPEAL = 10;
    public static final int MAX_AGRESSION_LEVEL = 10;
    public static final int MAX_STRENGTH = 10;
    private int id; 
    private int age; 
    private boolean gender; 
    private int sexAppeal; 
    private int agressionLevel; 
    private int strength; 
    private boolean isAlive;

    public Person (int id, int age, boolean gender, int sexAppeal, int agressionLevel, int strength, boolean isAlive){
        this.setId(id);
        this.setAge(age); 
        this.setGender(gender);
        this.setSexAppeal(sexAppeal); 
        this.setAgressionLevel(agressionLevel);
        this.setStrength(strength);
        this.setAlive(isAlive);
    }

    void getBorn () {
        isAlive = true; 
        age = 0;

        // a new people is born 
        // age = 0 
        // other variables: random 
    }

    void die () { 
        isAlive = false;
        // people die when they reach the max age 
        // people die when being on the same cell as vulcanos
        // people can be murdered 
        // setAlive = false 
    }

    void murder () { 
        // when 2 people with min agression level on the same land ==> weakest one dies 

    }

    void move () {
        // method to make people move 
        // random (only to adjucant fields) 
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public boolean isGender() {
        return gender;
    }

    public void setGender(boolean gender) {
        this.gender = gender;
    }

    public int getSexAppeal() {
        return sexAppeal;
    }

    public void setSexAppeal(int sexAppeal) {
        this.sexAppeal = sexAppeal;
    }

    public int getAgressionLevel() {
        return agressionLevel;
    }

    public void setAgressionLevel(int agressionLevel) {
        this.agressionLevel = agressionLevel;
    }

    public int getStrength() {
        return strength;
    }

    public void setStrength(int strength) {
        this.strength = strength;
    }

    public boolean isAlive() {
        return isAlive;
    }

    public void setAlive(boolean isAlive) {
        this.isAlive = isAlive;
    }



}

这是我的“测试类”,我尝试创建20个实例:

import java.util.concurrent.ThreadLocalRandom;

public class test {
    public static void main(String[] args) {
        for (int i = 0; i < 20; i ++) {
            Person person(i) = new Person();
            person.setId(i);
            person.setAge(ThreadLocalRandom.current().nextInt(0, Person.MAX_AGE + 1));
            person.setGender((Math.random() < 0.5));
            person.setSexAppeal(ThreadLocalRandom.current().nextInt(0, Person.MAX_SEX_APPEAL + 1));
            person.setAgressionLevel(ThreadLocalRandom.current().nextInt(0, Person.MAX_SEX_APPEAL + 1));
            person.setStrength(ThreadLocalRandom.current().nextInt(0, Person.MAX_SEX_APPEAL + 1));
            person.setAlive(true);


        }


    }
}

但是,我在此行收到以下错误

Person person(i) = new Person();
  • 构造函数Person()未定义
  • 类型不匹配:无法从Person转换为int 我理解这些错误,但我不知道另一种方法来达到我想要实现的结果

1 个答案:

答案 0 :(得分:4)

您应该制作一个列表,然后将创建的人员添加到其中。

public class test {
  public static void main(String[] args) {
    List<Person> persons = new ArrayList<>(); // create a list to store the generated persons
    for (int i = 0; i < 20; i++) {
      Person person = new Person(); // generate a person
      person.setId(i);
      person.setAge(ThreadLocalRandom.current().nextInt(0, Person.MAX_AGE + 1));
      person.setGender((Math.random() < 0.5));
      person.setSexAppeal(ThreadLocalRandom.current().nextInt(0, Person.MAX_SEX_APPEAL + 1));
      person.setAgressionLevel(ThreadLocalRandom.current().nextInt(0, Person.MAX_SEX_APPEAL + 1));
      person.setStrength(ThreadLocalRandom.current().nextInt(0, Person.MAX_SEX_APPEAL + 1));
      person.setAlive(true);

      persons.add(person); /// add the generated person to the list

    }


  }
}

此外,如果你想在没有参数的情况下调用 Person 构造函数,那么类必须有一个不带参数的构造函数。

public Person() {}