如何从arraylists& amp;类

时间:2015-10-22 10:42:56

标签: java arrays class arraylist

我是一名IT学生,在业余时间我想用java创建一些应用程序,我试图在一个但不知道如何开始。我有这个想法,它是一个随机的玩家/团队生成器。

例如足球,我想要一个程序,它给我一个来自array的随机玩家。所以我认为我必须制作class

当它随机生成一个玩家时,它必须拥有合适的团队,

示例:

  

播放器:梅西。
     球队:FC Barcelona。
     作用:攻击者。

但我该如何开始?谁能在路上帮助我?

1 个答案:

答案 0 :(得分:-2)

//这是一个让你入门的例子:

private static String[] playerNames = new String[] {"Messi", "Ronaldo"};
private static String[] teamNames = new String[] {"FC Barcelona", "Chelsea"};
private static String[] roleNames = new String[] {"Attacker", "Defender"};

public static void main(String[] args) {
    public List<Player> players;
    players = new ArrayList<Player>();
    players.add(new Player("Messi", "FC Barcelona", "Attacker"));
}

public Player getRandomPlayer() {
    Player player=null;
    String name = getRandomName();
    String team = getRandomTeam();
    String role = getRandomRole();
}

private String getRandomName() {
    return playerNames[new Random().nextInt(playerNames.length())];
}

private String getRandomTeam() {
    return teamNames[new Random().nextInt(teamNames.length())];
}

private String getRandomRole() {
    return roleNames[new Random().nextInt(roleNames.length())];
}

// class Player

public class Player {
    public String name;
    public String team;
    public String role;
    public Player(String name, String team, String role) {
        this.name = name;
        this.team = team;
        this.role = role;
    }
}