2011-11-20 70 views
2

基本上我試圖讓我的頭腦創建一個管理團隊和球員的Java程序。團隊和玩家對象Java困難

從我的理解我會有一個團隊和一個球員類。在團隊類中會有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); 
} 
} 
+4

你應該發佈一些說明你的問題的代碼。 – Dave

+1

如果您發佈您的代碼,這將有所幫助,這樣我們就可以看到發生了什麼問題。 –

回答

2

你的結構應該是完全正確的這種方式(和順便說一句,「一個到許多關聯「用」1:n「或」1:(1..n)「來描述)。

要麼你確定在你的代碼的某個地方有一個bug(例如一個靜態字段或者使用了一個標識符兩次),或者你可能會遇到一個ArrayList的問題(嘗試使用LinkedList進行測試) ,但我不確定。

編輯: 你忘了發表您的模型中,我們只看到了它的測試,但你已經得到了一些錯誤:

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

到目前爲止,一切都很好。除了它的毫無意義的,與空teamName創建團隊的一個實例,然後再把復位,但NVM ....

DivisionOne.addTeam(new Team(teamName)); 

Babam,你不加入以上DivisionOne,您所創建的團隊情況下,不你正在創建一個新的。其實,這是錯誤編號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); 

再次,你只設置你的團隊的第一個實例的新teamName,然後your're創造DivisionOne一個新的團隊。到目前爲止,Bug No.3;) 但是,您正在將一些新玩家添加到「舊」團隊實例中,與上面相同。

總而言之,您創建的「團隊」實例與您的DivisionOne無關。所以,你創建了一個Team的實例,把所有六個參與者放在一起,並且你調用了兩次showTeamPlayers。毫不奇怪畢竟,前3級的玩家依然在那裏....

最後一點:

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

應該

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

自變量從未與一個大寫字母開始,「DivisionOne」也可以是一個靜態類(因爲班級總是以人均字母開頭......)

+0

謝謝你,只是想知道,例如我怎麼能過來這些錯誤?如果您可以嘗試解釋第一個bug,那麼我可以向您提供建議並嘗試解決其他錯誤。其實有人毆打你,無論如何,謝謝! – mjsey

0

我沒有試圖得到這個工作,後患無窮。我遇到的一個特殊的 問題是,我每次創建一個團隊對象 並添加一個玩家對象,然後創建另一個團隊對象和 另一個球員,但如果我列出該新球隊的球員它顯示 以前的球員加入第一隊以及新球員。

確保團隊對象不共享任何字段。也許你正在使用一個「靜態」字段來存放玩家名單?如果你聲明一個靜態字段,它將在所有團隊實例中共享,這可能不是你想要的。

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")); 
    } 
} 
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)); 
     } 
    } 
} 
2

好的錯誤已經被PaddyG提及。這裏是溶液:

替換此代碼:

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

+0

感謝那 – mjsey

+0

你還需要創建一個新的團隊實例來移除已經存在的團隊? – mjsey

+0

沒有。但必須有辦法通過使用團隊名稱找到團隊,然後在ArrayList中搜索它。假設您的DivisionOne正在將團隊存儲在HashMap 中,其中關鍵字String是團隊名稱,值是團隊。然後你可以通過說'teamMap.remove(teamName)'來輕鬆刪除它。 – havexz