Java - 如何为链接的构造函数创建随机值

时间:2015-04-26 16:26:37

标签: java constructor

我试图在我的人类中编写第二个构造函数,将随机值发送到原始构造函数。我的问题是创建随机值。 Currentyl我一直试图在一行中创建它们(它们被发送到原始构造函数的行)。但是,这样做会导致编译错误,我不知道如何解决单个错误。

以下是代码:

import java.lang.Math;

public class Human{
    private String name;
    private int age;

    public Human(int ageIn, String nameIn){
        this.name = nameIn;
        this.age = ageIn;
    }
    public Human(){
        this(int (int)Math.round(Math.random();*100),String {"Beäbuä","Shun", "Kalle", "Pelle","Jorpi","Lelle","Cilla", "Basse","Sebbe","Nisse","Lasse","Paow<3"}[(int)Math.round(Math.random()*(names.length-1)))];
        changeRandom();
    }
    private static void changeRandom(){
        tal1 = Math.random();
        randomAge = (int)Math.round(tal1*100);
        tal2 = Math.random();
        index = (int)Math.round(tal2*(names.length-1));
        randomName = names[index];
    }

    public String toString(){
        return "namn: " + name +", ålder: "+age+" år.";
    }

    public int getAge(){
        return age;
    }

    public String getName(){
        return name;
    }
}

以下是错误消息:

Test.java:5: error: illegal start of expression
        name = {"Beäbuä","Shun", "Kalle", "Pelle","Jorpi","Lelle","Cilla", "Basse","Sebbe","Nisse","Lasse","Paow<3"}[1]
               ^
Test.java:5: error: not a statement
        name = {"Beäbuä","Shun", "Kalle", "Pelle","Jorpi","Lelle","Cilla", "Basse","Sebbe","Nisse","Lasse","Paow<3"}[1]
                ^
Test.java:5: error: ';' expected
        name = {"Beäbuä","Shun", "Kalle", "Pelle","Jorpi","Lelle","Cilla", "Basse","Sebbe","Nisse","Lasse","Paow<3"}[1]
                        ^
Test.java:5: error: illegal start of type
        name = {"Beäbuä","Shun", "Kalle", "Pelle","Jorpi","Lelle","Cilla", "Basse","Sebbe","Nisse","Lasse","Paow<3"}[1]
                                                                                                                    ^
Test.java:5: error: ';' expected
        name = {"Beäbuä","Shun", "Kalle", "Pelle","Jorpi","Lelle","Cilla", "Basse","Sebbe","Nisse","Lasse","Paow<3"}[1]
                                                                                                                     ^
Test.java:5: error: illegal start of type
        name = {"Beäbuä","Shun", "Kalle", "Pelle","Jorpi","Lelle","Cilla", "Basse","Sebbe","Nisse","Lasse","Paow<3"}[1]
                                                                                                                      ^
Test.java:6: error: ';' expected
        System.out.println(name);
              ^
Test.java:6: error: <identifier> expected
        System.out.println(name);
                          ^
Test.java:6: error: <identifier> expected
        System.out.println(name);
                               ^
Test.java:9: error: class, interface, or enum expected
}
^
10 errors

任何人都可以帮我解决这个问题,或者可能想出另一个解决方案,将随机字符串从列表发送到原始构造函数吗?

2 个答案:

答案 0 :(得分:0)

您可以创建一个String&amp;列表传递列表对象。

public Human(int ageIn, List<String> nameIn){
        this.name = nameIn;
        this.age = ageIn;
    }

主要方法

List<String> nameIn = new ArrayList<String>();
nameIn.add("Beäbuä"); // keep on adding the list in this way 
//say age is 5
Human human =new Human(5,nameIn);

如果您想以随机顺序输入字符串,那么您可以

nameIn.shuffle();
 Human human =new Human(5,nameIn);

以这种方式输入数据可以让您更好地控制代码。

答案 1 :(得分:0)

您需要创建人类字符串的数组列表。 所以像这样

private String[] names = {"Beäbuä","Shun", "Kalle", "Pelle","Jorpi","Lelle","Cilla", "Base","Sebbe","Nisse","Lasse","Paow<3"};

然后在你需要的构造函数中

public Human(){
    name = names[(int)(Math.random()*names.length)];
}