简体   繁体   中英

Java - How to create random values for linked constructor

I'm trying to write a second constructor to my human class that sends random values to the original constructor. My problem is creating the random values. Currentyl I've been trying to create them in one single line (the line where they are sent to the original constructor). However, I get a load of compiling errors from doing this, and I don't know how to solve a single one.

Here is the code:

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;
    }
}

and here is the error message:

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

Can anyone help me fix this, or possibly come up with another solution for sending random Strings from a list to the original constructor?

You can create a List of String & pass the list object.

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

In the main method

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);

If you want to make the Strings input in a random order then you can do

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

Inputting the data this way will provide you more control over your code.

You need to make an array list of human string things. so something like this

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

then in the constructor you need

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM