【问题标题】:Adding to an object in Java [closed]添加到Java中的对象[关闭]
【发布时间】:2016-02-14 01:22:52
【问题描述】:

我创建了以下数组列表:

premierLeague = new ArrayList<FootballClub>();

我有以下代码创建一个 FootballClub 对象并将其放入数组列表中:

System.out.println("Name of club:");
String name = input.next();
System.out.println("Location of club:");
String location = input.next();
FootballClub newClub = new FootballClub(name, location); // If only the name and location are placed in the new object, does that mean that all other stats are set to 0?
premierLeague.add(newClub);
System.out.println("Football club added! size = " + premierLeague.size());

我还有以下代码来遍历数组列表,以检查用户输入的 FootballClub 名称是否与数组列表中的 FootballClub 对象匹配。

Iterator<FootballClub> it = premierLeague.iterator();
while (it.hasNext()) {
    if (it.next().getClubName().equals(homeName)) {

    }
}

Football 类还有其他方法(除了namelocation),例如gamesWongamesLostPoints。如何允许用户输入足球俱乐部的名称和得分,然后将其添加到现有对象中?例如,如果用户输入 'Arsenal' 作为俱乐部,得分为 2,程序应找到名称为“Arsenal”的对象,然后将得分 2 添加到对象内的Pointsmethod。

FootballClub 类具有所有变量的 getter 和 setter。

【问题讨论】:

标签: java arrays oop


【解决方案1】:

遍历您的收藏,直到遇到一个名称与用户输入的名称相同的俱乐部,然后使用您匹配的俱乐部,调用 .addScore(2) 或其他方法。

【讨论】:

  • 没有问号,但他确实问了一个问题。我认为问号被省略了,可能是输入错误:“如何允许用户输入足球俱乐部的名称和得分,然后将其添加到现有对象中。”
猜你喜欢
  • 1970-01-01
  • 2011-11-25
  • 1970-01-01
  • 1970-01-01
  • 2021-06-20
  • 1970-01-01
  • 2012-03-07
  • 2014-03-12
  • 1970-01-01
相关资源
最近更新 更多