比较两个不同的arrayList

时间:2014-10-30 03:33:13

标签: java methods arraylist constructor

这是我第一次使用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.

1 个答案:

答案 0 :(得分:0)

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

这是一些伪代码。没有经过测试,但它应该让你知道我的意思。

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)
    }
}