基本上我試圖讓我的頭腦創建一個管理團隊和球員的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);
}
}
你應該發佈一些說明你的問題的代碼。 – Dave
如果您發佈您的代碼,這將有所幫助,這樣我們就可以看到發生了什麼問題。 –