但是,有128个足球队,当我打印出足球场的大小时……我只得到114个。有人可以帮我修复compareTo方法来解决此错误。我什至比较了多个字段,但对此不适应错误。
这是足球课
import java.util.*;
public class Football implements Comparable<Football>
{
public String name;
public int wins,finalRecord14,finalRecord13,finalRecord12,finalRecord11,bowlVictories,bowlLosses,ConferenceChamps;
public Integer points;
public Football(String name,int wins,int finalRecord14,int finalRecord13,int finalRecord12,int finalRecord11,int bowlVictories,int bowlLosses,int ConferenceChamp,Integer points)
{
this.name=name;
this.wins=wins;
this.finalRecord14=finalRecord14;
this.finalRecord13=finalRecord13;
this.finalRecord12=finalRecord12;
this.finalRecord11=finalRecord11;
this.bowlVictories=bowlVictories;
this.bowlLosses=bowlLosses;
this.ConferenceChamps=ConferenceChamp;
this.points=points;
}
public String getName()
{
return name;
}
public int getWins()
{
return wins;
}
public int getfinalRecord14()
{
return finalRecord14;
}
public int getfinalRecord13()
{
return finalRecord13;
}
public int getfinalRecord12()
{
return finalRecord12;
}
public int getfinalRecord11()
{
return finalRecord11;
}
public int getBowlVictories()
{
return bowlVictories;
}
public int getBowlLosses()
{
return bowlLosses;
}
public int getConferenceChamps()
{
return ConferenceChamps;
}
public Integer getPoints()
{
return points;
}
public int compareTo(Football o)
{
if(getPoints()>o.getPoints())
return 1;
else if(getPoints()<o.getPoints())
return -1;
else
{
if(getWins()>o.getWins())
return 1;
else if(getWins()<o.getWins())
return -1;
else
{
if(getConferenceChamps()>o.getConferenceChamps())
return 1;
else if(getConferenceChamps()<getConferenceChamps())
return -1;
else
{
if(getBowlVictories()>o.getBowlVictories())
return 1;
else if(getBowlVictories()<o.getBowlVictories())
return -1;
else
{
Random r=new Random();
int random=r.nextInt(2);
if(random==1)
return 1;
else
return -1;
}
}
}
}
}
}
这是时间表班
public class Schedule
{
public static void main (String args[])throws IOException
{
Scanner sc=new Scanner(new File("Footballteams.txt"));
Set<Football>teams=new TreeSet<Football>();
for(int i=0;i<114;i++)
{
String team=sc.nextLine();
Integer points=0;
String[]a=team.split("\\|",9);
String name=a[0];
int wins=Integer.parseInt(a[1]);
points+=wins;
int finalRecord14=Integer.parseInt(a[2]);
int finalRecord13=Integer.parseInt(a[3]);
int finalRecord12=Integer.parseInt(a[4]);
int finalRecord11=Integer.parseInt(a[5]);
int bowlVictories=Integer.parseInt(a[6]);
points=points+(bowlVictories*10);
int bowlLosses=Integer.parseInt(a[7]);
points=points-(bowlLosses*5);
int ConferenceChamp=Integer.parseInt(a[8]);
points=points+(ConferenceChamp*10);
Football x=new Football(name,wins,finalRecord14,finalRecord13,finalRecord12,finalRecord11,bowlVictories,bowlLosses,ConferenceChamp,points);
teams.add(x);
}
System.out.println(teams.size());
}
}
这是足球文本文件
Georgia Southern(Sun Belt)|9|0|0|0|0|0|0|1
Louisana-Lafayette(Sun Belt)|36|0|0|0|0|4|0|1
Appalachian State(Sun Belt)|7|0|0|0|0|0|0|0
Texas State(Sun Belt)|17|0|0|0|0|0|0|0
Arakansas State(Sun Belt)|35|0|0|0|0|2|2|3
South Alabama(Sun Belt)|14|0|0|0|0|0|1|0
Louisanna-Monroe(Sun Belt)|22|0|0|0|0|0|1|0
Troy(Sun Belt)|17|0|0|0|0|0|0|0
New Mexico State(Sun Belt)|9|0|0|0|0|0|0|0
Idaho(Sun Belt)|5|0|0|0|0|0|0|0
Georgia State(Sun Belt)|1|0|0|0|0|0|0|0
Memphis(AAC)|19|0|0|0|25|1|0|1
Cincinnati(AAC)|38|25|0|0|0|2|2|3
Central Florida(AAC)|36|0|0|10|0|2|1|2
East Carolina(AAC)|31|0|0|0|0|1|2|0
Houston(AAC)|34|18|0|0|0|2|1|0
Temple(AAC)|21|0|0|0|0|1|0|0
South Florida(AAC)|14|0|0|0|0|0|0|0
Tulane(AAC)|14|0|0|0|0|0|1|0
Tulsa(AAC)|24|0|0|0|0|1|1|1
Southern Methodist(AAC)|21|0|0|0|0|2|0|0
Connecticut|15|0|0|0|0|0|0|0
Florida State(ACC)|48|23|10|1|5|3|1|3
Clemson(ACC)|42|22|11|8|15|3|1|1
Louisville(ACC)|39|0|13|15|24|2|2|1
Boston College(ACC)|20|0|0|0|0|0|2|0
North Carolina State(ACC)|26|0|0|0|0|2|1|0
Syracuse(ACC)|23|0|0|0|0|2|0|1
Wake Forest(ACC)|18|0|0|0|0|0|1|0
Georgia Tech(ACC)|33|0|0|0|8|2|2|0
Duke(ACC)|28|0|0|23|0|0|2|0
North Carolina(ACC)|28|0|0|0|0|1|2|0
Pittsburgh(ACC)|25|0|0|0|0|1|3|0
Miami(ACC)|28|0|0|0|0|0|2|0
Virginia Tech(ACC)|33|21|0|0|0|2|2|0
Virginia(ACC)|19|0|0|0|0|0|1|0
Baylor(XII)|40|13|0|13|7|2|2|2
Texas Christian(XII)|44|14|0|0|3|2|1|2
Kansas State(XII)|38|15|12|0|18|0|3|1
Oklahoma(XII)|39|16|15|6|0|2|2|1
Texas(XII)|31|0|19|0|0|2|2|0
West Virginia(XII)|28|17|0|0|0|1|2|1
Oklahoma State(XII)|37|3|0|17|0|3|1|1
Texas Tech(XII)|25|0|0|0|0|2|0|0
Kansas(XII)|9|0|0|0|0|0|0|0
Iowa State(XII)|17|0|0|0|0|0|2|0
Ohio State(B1G)|44|0|3|12|1|2|2|1
Michigan State(B1G)|42|11|0|3|5|4|0|1
Maryland(B1G)|20|0|0|0|0|0|2|0
Rutgers(B1G)|32|0|0|0|0|2|2|0
Michigan(B1G)|31|12|24|0|0|1|2|0
Penn State(B1G)|31|0|0|0|0|1|1|0
Indiana(B1G)|14|0|0|0|0|0|0|0
Wisconsin(B1G)|39|10|0|22|13|1|3|2
Minnesota(B1G)|25|0|0|0|0|0|3|0
Nebraska(B1G)|37|24|25|0|0|1|3|0
Iowa(B1G)|26|0|0|0|0|0|3|0
Illinois(B1G)|19|0|0|0|0|1|1|0
Northwestern(B1G)|26|0|17|0|0|1|1|0
Purdue(B1G)|17|0|0|0|0|1|1|0
Marshall(CUSA)|35|0|0|0|23|3|0|1
Middle Tennessee(CUSA)|24|0|0|0|0|0|1|0
Western Kentucky(CUSA)|30|0|0|0|0|1|1|0
Alabama-Birmingham(CUSA)|14|0|0|0|0|0|0|0
Old Dominion(CUSA)|6|0|0|0|0|0|0|0
Florida-International(CUSA)|16|0|0|0|0|0|1|0
Florida-Atlantic(CUSA)|13|0|0|0|0|0|0|0
Louisanna Tech(CUSA)|30|0|0|0|0|1|1|1
Rice(CUSA)|29|0|0|0|0|2|1|1
Texas-El Paso(CUSA)|17|0|0|0|0|0|1|0
Texas-San Antonio(CUSA)|19|0|0|0|0|0|0|0
North Texas(CUSA)|22|0|0|0|0|1|0|0
Southern Mississippi(CUSA)|16|20|0|0|0|1|0|1
Bowling Green(MAC)|31|0|0|0|0|1|2|1
Ohio(MAC)|32|0|0|0|0|2|1|0
Buffalo(MAC)|20|0|0|0|0|0|1|0
Akron(MAC)|12|0|0|0|0|0|0|0
Massachusetts(MAC)|5|0|0|0|0|0|0|0
Miami(MAC)|10|0|0|0|0|0|0|0
Kent State(MAC)|22|0|0|0|0|0|1|0
Northern Illinois(MAC)|46|0|22|0|0|1|3|3
Toledo(MAC)|34|0|0|0|0|2|1|0
Western Michigan(MAC)|20|0|0|0|0|0|2|0
Central Michigan(MAC)|23|0|0|0|0|0|2|0
Ball State(MAC)|30|0|0|0|0|0|2|0
Eastern Michigan(MAC)|12|0|0|0|0|0|0|0
Boise State(MWC)|43|8|18|0|16|3|0|2
Colorado State(MWC)|25|0|0|0|0|1|1|0
Utah State(MWC)|37|0|16|0|0|3|1|1
Air Force(MWC)|25|0|0|0|0|1|2|0
New Mexico(MWC)|12|0|0|0|0|0|0|0
Wyoming(MWC)|21|0|0|0|0|0|1|0
Fresno State(MWC)|30|0|0|0|0|0|3|2
San Diego State(MWC)|32|0|0|0|0|1|3|1
Nevada(MWC)|25|0|0|0|0|0|3|0
Hawaii(MWC)|14|0|0|0|0|0|0|0
San Jose State(MWC)|25|0|21|0|0|1|0|0
Nevada-Las Vegas(MWC)|13|0|0|0|0|0|1|0
Oregon(PAC12)|48|4|2|9|2|4|1|2
Stanford(PAC12)|42|7|7|11|0|2|2|2
Washington(PAC12)|31|0|0|25|0|1|3|0
California(PAC12)|16|0|0|0|0|0|1|0
Oregon State(PAC12)|24|0|20|0|0|1|1|0
Washington State(PAC12)|16|0|0|0|0|0|1|0
Arizona(PAC12)|30|0|0|0|19|2|1|0
California-Los Angeles(PAC12)|35|0|0|16|10|2|2|0
Arizona State(PAC12)|34|0|0|21|12|2|2|0
Southern California(PAC12)|36|6|0|19|20|2|1|0
Utah(PAC12)|27|0|0|0|21|2|0|0
Colorado(PAC12)|10|0|0|0|0|0|0|0
Missouri(SEC)|36|0|0|5|14|3|0|0
Georgia(SEC)|40|19|5|0|9|2|1|0
Florida(SEC)|29|0|9|0|0|1|1|0
Tennessee(SEC)|22|0|0|0|0|1|0|0
South Carolina(SEC)|40|9|8|4|0|4|0|0
Kentucky(SEC)|14|0|0|0|0|0|0|0
Vanderbilt(SEC)|27|0|23|24|0|2|1|0
Alabama(SEC)|48|1|1|7|4|2|2|2
Mississippi State(SEC)|32|0|0|0|11|2|2|0
Mississippi(SEC)|26|0|0|0|17|2|1|0
Auburn(SEC)|31|0|0|2|22|1|2|1
Louisiana State(SEC)|41|2|14|14|0|1|3|1
Texas A&M(SEC)|35|0|5|18|0|4|0|0
Arkansas(SEC)|25|5|0|0|0|2|0|0
Notre Dame(IND)|37|0|4|20|0|2|2|0
Brigham Young(IND)|34|0|0|0|0|2|2|0
Navy(IND)|30|0|0|0|0|2|1|0
Army(IND)|12|0|0|0|0|0|0|0
最佳答案
但是您只读取114行。
for(int i=0;i<114;i++)
{
String team=sc.nextLine();
Integer ...
每行没有多个团队,也没有要解析的代码。您可以使它成为动态的,而不是硬编码为读取114行。
关于java - 如何修复集合的compareTo方法?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28572295/