(编辑代码)我有一个问题,我希望我能得到一些帮助。以下是我的条件:
你正在开发一个程序来跟踪球队在联赛中的排名。当一场比赛进行时,获胜的球队(得分较高的球队)获得2分,而输球队则没有得分。如果双方平局,两队都能拿到1分。当两支球队之间的比赛结果被报告时,必须调整排名的顺序。下面的课记录一场比赛的结果。
public class GameResult
{
public String homeTeam() // name of home team
{ /* code not shown */ }
public String awayTeam() // name of away team
{ /* code not shown */ }
public int homeScore() // score for home team
{ /* code not shown */ }
public int awayScore() // score for away team
{ /* code not shown */ }
// instance variables, constructors, and other methods not shown
}每个团队的信息由类TeamInfo的一个实例存储,该类的部分定义如下。
public class TeamInfo
{
public String teamName()
{ /* code not shown */ }
public void increasePoints(int points)
{ /* code not shown */ }
public int points()
{ /* code not shown */ }
// instance variables, constructors, and other methods not shown
}类TeamStandings存储团队排名的信息。部分声明如下所示。
public class TeamStandings
{
TeamInfo[] standings; // maintained in decreasing order by points,
// teams with equal points can be in any order
public void recordGameResult(GameResult result)
{ /* to be completed as part (c) */ }
private int teamIndex(String name)
{ /* to be completed as part (a) */ }
private void adjust(int index, int points)
{ /* to be completed as part (B)/> */ }
// constructors and other methods not shown
}下面是实际的问题:
写方法调整。方法调整应将在排名中的索引位置上发现的团队积分增加到参数点所给的数量。此外,在索引中发现的队伍在排名中的位置应该被改变,以保持按分数递减的顺序;得分相等的球队可以出现在任何顺序。
到目前为止,我得到的是:
private void adjust(int index, int points)
{
int Score[] = new int[standings.length]
for ( int i=0; i < standings.length; i++)
{
Score[i] = points;
Arrays.sort(Score);
}
}我意识到这是非常错误的,需要一些指导来解决这个问题。谢谢!
发布于 2014-04-18 14:40:37
像这样的事情应该有效:
private void adjust(int index, int points) {
// increase points of winning team
TeamInfo curr = standings[index];
curr.increasePoints(points);
// get the new score of the winning team
int points = curr.points();
// perform an insertion sort on the modified portion
int i = index;
while (i > 0 && standings[i-1].points() < points) {
// shift teams with lower scores rightwards
standings[i] = standings[i-1];
i--;
}
standings[i] = curr;
}基本上,它只是在指定的curr参数下获得获胜团队( index ),并增加其点数。由于列表必须按团队点数降序排列,因此在调整点数后,只需插入团队正确的位置即可。
发布于 2014-04-18 14:16:43
问题是:
for ( int i=0; i <= standings.length; i++)//here index out of bounds
{
Score[i] = index, points;//here
}写得像:
for ( int i=0; i <standings.length; i++)
{
Score[i] = points;
}发布于 2014-04-18 14:42:14
以下是如何调整一支球队在积分榜上的得分。
private void adjust(int index, int points)
{
/* 'index' is by definition an index into the standings array
* 'points' is by definition how many points to give to the team at 'index'
* each TeamInfo object has a method called increasePoints()
* therefore, to increase the number of points for a team in the standings... */
standings[index].increasePoints(points);
}讲得通?
现在,为了按照点值的顺序排序排名,我想这个练习需要您做一些结合使用TeamStandings.teamIndex()和TeamInfo类中其他方法的事情。但是,由于代码要么是隐藏的,要么是尚未编写的,所以我不能做更多的事情。
https://stackoverflow.com/questions/23155966
复制相似问题