我正在做以下活动:目标是开发一个程序来管理跳远比赛中的成员列表。提供的名额数量为15个。他们的数据将按照运动员报名的相同顺序进行介绍。设计一个显示以下选项的程序:
1-注册参与者
2-列出所有参与者的数据
3-按标记列出所有参与者的数据
4-退出如果选择1,将介绍其中一个参与者的数据:姓名、2012年最佳分数、2011年最佳分数和2010年最佳分数。
如果选择2,我们必须列出所有参与者的数据,按背部编号排序(他们注册的顺序)
如果选择3,我们必须按2012标记列出所有参与者的数据,从大到小。
处理完每个选项后,必须再次显示主菜单,直到选择选项4,退出程序。
我被按标记排序数据卡住了,这是我的程序,有什么想法吗?
package prc;
import epsa.Cio;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Rrc2
{
public static void main(String[] args)
{
int n=0;
Tupla participants[]= new Tupla[15];
while(true){
Cio.println("Choose what you would like to do");
Cio.println("Type 1 to:Register a participant");
Cio.println("Type 2 to:List all the participant's data");
Cio.println("Type 3 to:List all the participants data by mark");
Cio.println("Type 4 to:Quit");
int i;
i = Cio.readInt();
switch(i)
{
case 1:
participants[n]= new Tupla();
Cio.println("Insert dorsal number");
participants[n].number = Cio.readInt();
Cio.println("Insert participants name");
participants[n].name = Cio.readString();
Cio.println("Insert participants best mark for the year 2012");
participants[n].bestMark2012 = Cio.readDouble();
Cio.println("Insert participants best mark for the year 2011");
participants[n].bestMark2011 = Cio.readDouble();
Cio.println("Insert participants best mark for the year 2010");
participants[n].bestMark2010 = Cio.readDouble();
n++;
break;
case 2:
int t;
for(t=0;t<15&&t<n;t++){
Cio.println("name: " + participants[t].name);
Cio.println("Dorsal number: " + participants[t].number);
Cio.println("Best Mark 2012: " + participants[t].bestMark2012);
Cio.println("Best Mark 2011: " + participants[t].bestMark2011);
Cio.println("Best Mark 2010: " + participants[t].bestMark2010);
Cio.println("//////////////////////////////////////////////////");
}
break;
case 3:
Collections.sort(participants);
break;
case 4:System.exit(0);
break;
default:
Cio.println("Not valid");
break;
}
Cio.println("Press enter to continue");
Cio.readString();
}
}
}
发布于 2014-01-20 07:54:01
您只需要实现java.util.Comparator<Tupla>
并将其传递到对Collections.sort()
的调用中(并且使用java.util.List
而不是数组)
Collections.sort(participants, new TuplaComparator());
您需要为TuplaComparator
实现(自己编写类)。它应该实现接口java.util.Comparator<Tupla>
。(请参阅javadoc。)
变量participants
应该是一个ArrayList
,而不是一个简单的数组,Collections.sort()
才能工作。
这有很多线索可以继续下去。我不会重现你的全部(作业?)我答案中的程序代码。
https://stackoverflow.com/questions/21224188
复制相似问题