【问题标题】:Comparing two different arrayList比较两个不同的arrayList
【发布时间】:2014-12-25 23:58:13
【问题描述】:

这是我第一次使用 stackFlow,我需要帮助 我对编程有点陌生。 我有一个我创建的方法,它询问球队每场比赛的得分和失分。我希望有人解释我需要做什么,仅此而已,或者如果不可能这样 我想将团队的名称存储在 arrayList 中。

我怎么问,什么是阿森纳得分,什么是阿森纳得分失分,以及利物浦得分和失分是什么?请,如果您不理解我的问题,请告诉我,以便我可以更好地改写它。谢谢。

public class projectscore
{
  public int pointsMade;
  public int pointsLost;
  public projectscore(int pointsM, int pointsL)
  {
   pointsMade = pointsM;`enter code here`
   pointsLost = pointsL;
  }

  public void setpointsMade(int pointsM)
  {
    pointsMade = pointsM;

  }
  public void setpointsLost(int pointsL)
  {

    pointsLost = pointsL;

  }
  public int getpointsMade()
  {
    return pointsMade;

  }
  public int getpointsLost()
  {

    return pointsLost;

  }
  public void getfinalScore()
  {

   System.out.println(pointsMade);
   System.out.println(pointsLost);
  }
}

import java.util.*;
public class fProjectScore
{
  public static void main(String[] args)
  {
    projectscore pscore = new projectscore(0, 0);
    Scanner in = new Scanner(System.in);


    System.out.println("what is the points made today ");
    Integer temp = in.nextInt();
    System.out.println("what is the points lost today ");
    Integer temp2 = in.nextInt();


    pscore.setpointsMade(temp);
    pscore.setpointsLost(temp2);

 /*   HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
    map.put(temp, temp2);
   */ 
    if(temp > temp2)
    {
      System.out.println("winner"); 
    }
    else  if(temp < temp2)
    {
      System.out.println("losser"); 
    }
    else  if(temp == temp2)
    {
      System.out.println("Tie Game"); 
    }
}
}

i want to add this to my main method 
 ArrayList<String>participants = new ArrayList<String>();
participants.add("Arsenal");
participants.add("Liverpool);

how do i get it to ask, What is Arsenal pointsMade, What is Arsenal pointsLost, and What it liverpool pointsMade and pointsLost? please, if you dont understand my question, let me know so i can rephrase it better. Thanks.

【问题讨论】:

  • 是的,我不明白,请多解释一下?
  • 如何让方法看到 arrayList 参与者?我想用arrayList中的元素来问“阿森纳制造”有什么意义? “阿森纳失落”有什么意义?所以对于arrayList 中的每个元素,我希望提出问题。英语真的不是我的第一语言,所以我很难深入解释。如果我在 arraylist 中有 10 个元素,我希望它为每个元素提出问题。
  • 还是我走错路了?
  • 哦,我假设您听说过 Java API?您可以使用 ArrayList 上的那个来找出方法,这里是链接。 docs.oracle.com/javase/8/docs/api/java/util/…

标签: java methods arraylist constructor


【解决方案1】:

我建议您阅读面向对象的编程,并为团队创建一个类。

这是一些伪代码。未经测试,但它应该能让你明白我的意思。

public class Team {
    int points;
    final String name;

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

    public void setPoints(int p) { this.points = p; }
    public int getPoints() { return this.points; }
    public String getName() { return this.name; }
}

void main() {
    Team arsenal = new Team("Arsenal");
    Team liverpool = new Team("Liverpool");
    ArrayList<Team> teams = new ArrayList<Team>();
    teams.add(arsenal);
    teams.add(liverpool);

    Scanner in = new Scanner(System.in);
    for(Team team : teams) {
        System.out.println("How many points did " + team.getName() + " made today");
        Integer points = in.nextInt();
        team.setScore(points);
    }

    // Sort the teams by score somehow
    teams.sort()

    // Get the winners 
    ArrayList<List> winners = this.getWinners(teams)

    if (winners.count > 1) {
        // It's a draw between the ones in the winners arraylist
    } else {
        println("The winner is: " + winners.get(0).name)
    }
}

【讨论】:

    猜你喜欢
    • 2013-05-03
    • 1970-01-01
    • 2012-10-23
    • 2015-10-05
    • 2012-01-25
    • 2021-06-04
    • 2013-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多