奇怪的ArrayList值

时间:2018-07-20 23:01:15

标签: java arraylist

意图:该程序旨在接收用户的输入并将其与ArrayList的内容进行比较。如果找不到输入,则将输入的程序编程到列表中,并将winCount设置为1。将打印列表,并在列表中显示获胜数。最终,赢利最高的用户获胜。如果有平局,则随机选择一个用户。

问题:我的arrayValues没有正确存储

Team类提供了以下方法:

public class Team implements Comparable<Team> {
// _-v-_-v-_-v-_-v-_-v-_-v-_-v-_-v-_-v-_-v-_-v-_-v-_-v-_-v-_-v-_-v-_-v-_
// CS 1323: Implement this method
/**
 * This is the method you will fix to allow you to return the contents of
 * this Team item in the nice format: "name: winCount".  The current 
 * implementation will allow the program to compile, but it returns nothing.
 * 
 * For example: "Sooners: 3"
 * 
 * @return the formatted String that can then be output to the console.
 */
public String toString(String team, int wins) {
    String winStatement = team + ": " + wins;
    return winStatement;
}
// _-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_

// Data fields
private String name;
private int winCount;

Team() {
    name = "Sooners";
    winCount = 1;
}

Team(String inputName) {
    name = inputName;
    winCount = 1;
}

Team(String inputName, int inputWinCount) {
    name = inputName;
    winCount = inputWinCount;
}

// ----------------------------------------------------
// Getters and Setters (aka Accessors and Mutators) ===
/**
 * @return the name
 */
public String getName() {
    return name;
}

/**
 * @param name
 *            the name to set
 */
public void setName(String name) {
    this.name = name;
}

/**
 * @return the winCount
 */
public int getWinCount() {
    return winCount;
}

/**
 * @param winCount
 *            the winCount to set
 */
public void setWinCount(int winCount) {
    this.winCount = winCount;
}

/**
 * Increments the winCount variable by one for this Team
 */
public void incrementWinCount() {
    winCount++;
}

/**
 * This method allows you to check to see if this Team object has the same
 * name as another Team object.
 * 
 * This method allows you to use the contains method in ArrayList to see
 * if any element in an array list has the same name as a specific Team.
 * 
 * @param o
 *            the other Team being compared to.
 */
@Override
public boolean equals(Object o) {
    return name.equals(((Team) o).name);
}

/**
 * This method allows you to check to see if this Team object has the same
 * name as another Team object
 * 
 * @param otherTeam
 *            one team
 */
public boolean sameName(Team otherTeam) {
    return name.equals(otherTeam.name);
}
/**
 * This method allows you to check to see if this Team object has the same
 * name as another Team object
 * 
 * @param team1
 *            one team
 * @param team2
 *            the other team
 */
public static boolean sameName(Team team1, Team team2) {
    return team1.name.equals(team2.name);
}

/**
 * This method allows you to sort an ArrayList of Team items using
 * Collections.sort
 * 
 * @param o
 *            the other Team being compared to.
 * @return -1 if this Team item should come first, +1 if this Team item
 *         should come after the other, and 0 if this Team item is
 *         equivalent to the other.
 */
@Override
public int compareTo(Team o) {
    if (this.winCount < o.winCount) {
        return -1;
    } else if (this.winCount > o.winCount) {
        return 1;
    }
    return 0;
    }

}

我是引用变量和ArrayList的新手,所以我怀疑该错误与它们有关。代码如下:

  public class Project6_ReeceWitcher
  {
      public static void main(String args[]) 
      {

    Scanner scnr = new Scanner(System.in);
    Random rando = new Random();
    String name = "hi";
    int cycles = 0;
    int value = 0;
    ArrayList<Team> teams = new ArrayList<Team>();
    Team myTeam = new Team();
    Team thisTeam = new Team(name);
    System.out.println("Welcome to the Advanced Sportsball Tracker!");

    while (!name.equals("x")) // looping print statement
    { // x loop begins
        System.out.println("Which team just won? (x to exit)");
        name = scnr.next();
            if (!teams.equals(name))
                {
                teams.add(thisTeam);
                myTeam.setWinCount(1);
                }
            else if (teams.equals(name))
            {
                myTeam.incrementWinCount();
            }
        cycles++;
    }// x loop ends

    if (cycles == 1) // prints no data if user immediately exits
    {
        System.out.println("No data input");
    }

    System.out.println("Final Tally: "); //  loop to print teams 
    for (Team team : teams) // FIXME
    {
        System.out.println(team);
    }

程序运行并输出这些值

欢迎使用Advanced Sportsball Tracker!

哪个团队刚刚获胜? (x退出)

1 哪支球队刚刚获胜? (x退出)

2 哪支球队刚刚获胜? (x退出) X 最终提示: 团队@ 75b84c92 团队@ 75b84c92 团队@ 75b84c92 胜利者是有赢的

但是预期的输出是

欢迎使用Advanced Sportsball Tracker! 哪支球队刚刚获胜? (x退出) 很快-这些值是用户输入的,不会打印 哪支球队刚刚获胜? (x退出) 很快- 哪支球队刚刚获胜? (x退出) 牛仔 - 哪支球队刚刚获胜? (x退出) 熊- 哪支球队刚刚获胜? (x退出) 熊-
哪支球队刚刚获胜? (x退出) 牛仔-
哪支球队刚刚获胜? (x退出) ThatOtherTeam-
哪支球队刚刚获胜? (x退出) X 最终提示: ThatOtherTeam:1 较快人数:2 牛仔:2 熊:2 赢家是牛仔,赢得2次胜利!

谢谢您的帮助!

0 个答案:

没有答案