1

如何创建记录 2 个不同团队之间的赢/输的方法?这是团队对象和构造函数。

public class NFLTeam4
{   private int win;
    private int loss;
    private String team;

public NFLTeam4(String teamName)
{   win = 0;
    loss = 0;
    team = teamName;
}
public NFLTeam4(int w, int l)
{   win = w;
    loss = l;
}
}

这是我尝试创造一种赢得比赛的方法。

public class NFLGameDay4{
   public static void main(String[] args){  
    NFLTeam4 falcons = new NFLTeam4("Falcons");
    NFLTeam4 chiefs = new NFLTeam4("Chiefs");
    NFLTeam4 jets = new NFLTeam4("Jets");
}       

public NFLTeam4 winAGame(NFLTeam4 b)
{   int win = 1;
    int loss = 0;
    return new NFLTeam4(win, loss);
}   
4

2 回答 2

1

首先,看起来你的NFLTeam4班级能够代表一个团队......

public NFLTeam4(String teamName) {   
    win = 0;
    loss = 0;
    team = teamName;
}

...或团队的记录...

public NFLTeam4(int w, int l) {   
    win = w;
    loss = l;
}

但不能同时提供:无法预先提供团队名称,然后再修改记录。要纠正这一点(从而使管理团队的记录成为可能),请考虑删除第二个构造函数(第int w, int l一个)并创建两种方法来为您处理更改记录:

public void addWin() { 
    ++win;
}

//similar code for addLoss

请注意,您可能需要添加类似getWinCount的方法。您要添加什么以及如何编写代码取决于您,这只是为了让您入门。

现在winAGame只需要调用addWin提供的团队 ( NFLTeam4 b)。

于 2012-10-04T03:15:29.080 回答
0

Map您可以在NFLTeam4课堂上创建一个结果。

public class NFLTeam4
{
  private enum RESULT { WIN, LOSS };
  private Map<RESULT, List<NFLTeam4>> results = new HashMap<RESULT, ArrayList<NFLTeam4>>;
  // you can leave the constructor and other variables as they are

  public void winAGame(NFLTeam4 b) {
    win = win + 1;
    // Now check and add to your results map 
    // you will need to make sure your map is initialised properly
    results.get(RESULT.WIN).add(b);
  }
}

你在你的 NFLTeam4 对象中封装了一场比赛的输赢行为。您还可以扩展RESULT枚举以包含有关结果的日期、比赛编号、地点等的一些信息。

这只是说明它是如何工作的。您应该考虑如何根据要求设计对象。

于 2012-10-04T03:15:03.500 回答