团队和玩家反对Java困难

时间:2011-11-20 20:12:36

标签: java object

基本上我正试图创建一个管理团队和玩家的java程序。

根据我的理解,我会有一个团队和一个球员类。在团队类中,会有get和set方法,以及用于存储右边玩家的某种形式的集合,例如数组列表?然后在播放器类中使用相关的get和set方法。

这种设置是因为一支球队有一对多的球员吗?

我没有遇到麻烦试图让这个工作。我遇到的一个特殊问题是,每次我创建一个团队对象,并向其添加一个玩家对象,然后创建另一个团队对象和另一个玩家,但如果我列出该新团队的玩家,则会显示之前添加的玩家第一支球队以及新球员。

所以我认为它已经回到了绘图板,并想知道是否有人可以提供一些关于如何构建它的一般性建议?

非常感谢,

import java.util.Iterator;

public class test {
public test() {
}

//Method to show the team and its players

public static void showTeamPlayers(Team aTeam) {
    Player players;
    System.out.println(aTeam.getTeamName());
    Iterator e = aTeam.getPlayerList().iterator();
    while (e.hasNext()) {
        players = (Player)e.next();
        System.out.println("\t" + players.getPlayerNumber() + " " + players.getPlayerName());
    }
    System.out.println("");
}

public static void main(String[] args) {
    int teamID;
    String teamName = "";

    //First create a divison/league
    League DivisionOne = new League("Division One");

    //Create a new team object
    Team team = new Team(teamName);

    //Asks the user to enter a team name and stores the input
    UserInput.print("Enter team name:");
    teamName = UserInput.readString();

    team.setTeamName(teamName);

    //Add the team
    DivisionOne.addTeam(new Team(teamName));

    Player player = new Player(0, "Dave");
    Player player1 = new Player(1, "Dennis");
    Player player2 = new Player(2, "Peter");

    //Add to team
    team.addPlayer(player);
    team.addPlayer(player1);
    team.addPlayer(player2);

    test.showTeamPlayers(team);

    //Asks the user to enter a team name and stores the input
    UserInput.print("Enter team name:");
    teamName = UserInput.readString();

    team.setTeamName(teamName);

    //Add the team
    DivisionOne.addTeam(new Team(teamName));

    Player player3 = new Player(3, "Creamer");
    Player player4 = new Player(4, "Matt");
    Player player5 = new Player(5, "John");

    //Add to team 1
    team.addPlayer(player3);
    team.addPlayer(player4);
    team.addPlayer(player5);

    test.showTeamPlayers(team);
}
}

5 个答案:

答案 0 :(得分:2)

你的结构应该是完全正确的(并且btw,“一对多关联”用“1:n”或“1:(1..n)”来描述)。

你的代码中肯定有一个bug(发布它;)),例如静态字段或使用两次标识符,或者你可能遇到一个ArrayList问题(尝试使用LinkedList进行测试),但我不确定。

编辑: 你忘了发布你的模型,我们只看到它的测试,但你已经有一些错误:

Team team = new Team(teamName);
teamName = UserInput.readString();
team.setTeamName(teamName);

到目前为止,这么好。除了它毫无意义,用一个空的teamName创建一个Team的实例然后重置它,但是nvm ....

DivisionOne.addTeam(new Team(teamName));

Babam,你不是将你创建的Team实例上面添加到DivisionOne,不是你要创建一个新实例。实际上,那就是Bug No.1

team.addPlayer(player);
team.addPlayer(player1);
team.addPlayer(player2);

但是你把新玩家放到你上面创建的实例中,他们没有进入为DivisionOne创建的团队......如果你想要的话,错误的第2个......然后

team.setTeamName(teamName);
DivisionOne.addTeam(new Team(teamName));
.
.
.
team.addPlayer(player3);
team.addPlayer(player4);
team.addPlayer(player5);

同样,您只需要为您的第一个Team实例设置一个新的teamName,然后您将为DivisionOne创建一个新的Team。到目前为止,Bug No.3;) 但是你要把一些新玩家加入“老”团队实例,与上面相同。

总而言之,您创建的“团队”实例与您的DivisionOne无关。所以,你创建了一个Team的实例,将六个玩家全部放在一起,你就可以调用2次showTeamPlayers。毫无疑问,前三名球员仍在那里......

最后一点:

League DivisionOne = new League("Division One");

应该是

League divisionOne = new League("Division One");

由于变量永远不会以大写字母开头,因此“DivisionOne”也可以是静态类(因为类总是以人均字母开头......)

答案 1 :(得分:2)

PaddyG已经提到了很多错误。这是soln:

替换此代码:

teamName = UserInput.readString();

team.setTeamName(teamName);


//Add the team
DivisionOne.addTeam(new Team(teamName));

使用:

teamName = UserInput.readString();

team = new Team(teamName);

//Add the team
DivisionOne.addTeam(team);

并取代:

teamName = UserInput.readString();

team.setTeamName(teamName);

//Add the team
DivisionOne.addTeam(new Team(teamName));


Player player3 = new Player(3, "Creamer");
Player player4 = new Player(4, "Matt");
Player player5 = new Player(5, "John");

使用:

teamName = UserInput.readString();

team = new Team(teamName);

//Add the team
DivisionOne.addTeam(team);


Player player3 = new Player(3, "Creamer");
Player player4 = new Player(4, "Matt");
Player player5 = new Player(5, "John");

正如您在上面的代码中所看到的那样,我们使用新团队的新实例更新了team变量。此新实例已添加到DivisionOne。当您执行DivisionOne.addTeam(new Team(teamName));时,您正在创建并添加一个全新的实例  DivisionOne但是您添加玩家的实例是另一个(由team变量保存)。所以soln是创建一个新实例并使用这个新创建的实例设置变量team,然后将玩家添加到它并将其添加到DivisionOne

答案 2 :(得分:0)

  

我没有遇到麻烦试图让这个工作。一个特别的   我遇到的问题是,每次我创建一个团队对象,   并向其添加一个玩家对象,然后创建另一个团队对象   另一名球员,但如果我列出了新球队的球员,那就表明了   之前的球员加入了第一支球队以及新球员。

确保团队对象不共享任何字段。也许你在使用“静态”字段来保存玩家的列表?如果您声明一个静态字段,它将在所有团队实例之间共享,这可能不是您想要的。

答案 3 :(得分:0)

看到你的代码会有所帮助,但鉴于你的描述,我想从这样的东西开始作为例子:

// Team.java
public class Team {
   private String name;
   private List<Player> players;

   public Team(String name) {
      this.name = name;
      this.players = new ArrayList<Player>();
   }

   public String getName() {
       return name;
   }

   public List<Player> getPlayers() {
       return players;
   }
}

// Player.java
public class Player {
   private String name;

   public Player(String name) {
      this.name = name;
   }

   public String getName() {
      return name;
   }
}

// Main.java
public class Main {
   public static void main(String[] args) {
      Team team1 = new Team("Team #1");
      Team team2 = new Team("Team #2");
      team1.getPlayers().add(new Player("Bob"));
      team2.getPlayers().add(new Player("Joe"));
   }
}

答案 4 :(得分:0)

我同意@Kaleb的回答,但我会给你一个替代方案(如果你想的话)......

public class Player {
    private String name;
    protected int speed;
    protected int health;

    public Player(String name, int speed, int health) {
        this.name = name;
        this.speed = speed;
        this.health = health;
    }
}

public class Main {
    public static void main(String[] args) {
        Map<Player> team1 = new HashMap<Player>();
        Map<Player> team2 = new HashMap<Player>();
        System.out.print("Enter the name of the player followed by its speed, health, and team number:");
        java.util.Scanner sc = new java.util.Scanner(System.in).useDelimiter(",");
        String name = sc.next();
        int speed = sc.nextInt();
        int health = sc.nextInt();
        if (sc.nextInt() == 1) {
            team1.put(new Player(name, speed, health));
        } else {
            team2.put(new Player(name, speed, health));
        }
    }
}