我正在制作一个拼字游戏应用程序,并且想要创建一个最高分系统。我开始制作一个数组,在其中保存玩家的姓名及其相应的分数。我想对分数数组进行排序,以便它们从最高到最低排列,并输出分数(降序排列)以及相应的名称。请指导我如何对分数进行排序并输出分数以及相应的球员姓名。任何帮助将非常感激。谢谢!
这是我到目前为止的内容:
public class MainTop extends Activity {
TextView show;
EditText name;
EditText score;
Button ok;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layouttop);
show = (TextView) findViewById(R.id.textView1);
name = (EditText) findViewById(R.id.editText1);
score = (EditText) findViewById(R.id.editText2);
ok = (Button) findViewById(R.id.button1);
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String [] namearray = new String [4];
int [] scorearray = new int[4];
for (int i = 0; i < 4; i++){
String playername = name.getText().toString();
namearray[i] = playername;
String playerscorestr = score.getText().toString();
int playerscore = Integer.parseInt(playerscorestr);
scorearray[i] = playerscore;
show.setText(namearray[i] + scorearray[i]);
}
最佳答案
我没有关系分数和球员的地方,但是如果您的分数和球员是这样的,那么您可以尝试一下
int[] scores={10,50,20,60};
String[] players={"player1","player2","player3","player4"};
Hashtable<String, String> playerdetails=new Hashtable<String, String>();
ArrayList scoreslist=(ArrayList) Arrays.asList(scores);
for(int i=0;i<scores.length;i++)
{
playerdetails.put(String.valueOf(scores[i]), players[i]);
}
Collections.sort(scoreslist);
for(int i=0;i<scoreslist.size();i++)
{
show.setText(scoreslist.get(i)+" "+playerdetails.get(scoreslist.get(i)));
}