この配列を対応する名前で最高から最低まで並べ替えることができます。配列のスコアを最低から最高まで短くする方法を作ったが、一緒にプリントアウトするために名前とスコアを結びつける方法を見つけるのが難しい。 は、あなたがオブジェクトを含む、二つの平行アレイを使用して、代わりに単一のものを使用しているという問題があるJava配列:スコアをソートして適切な名前で表示する
import java.util.*;
/**
* This program will prompt the user to enter the number of game players,
* the game players' names, and their scores, and prints game players name
* and scores in decreasing order of their scores.
*
*/
//class name
public class SortGameScores
{
// main program start
public static void main(String[] args)
{
//declaring variable
int numberOfPlayers;
//create new scanner
Scanner input = new Scanner(System.in);
//Ask user to enter number of players and record number
System.out.print("Enter number of players: ");
numberOfPlayers = input.nextInt();
//Create and declare arrays using previous input
int[] player = new int [numberOfPlayers];
String[] name = new String[numberOfPlayers];
int[] score = new int [numberOfPlayers];
//Ask user to enter each player's name
for (int i = 0; i < player.length; i++)
{
System.out.print("Enter player's name: ");
name[i] = input.next();
}
//Ask user to enter each player's score
for (int i = 0; i < player.length; i++)
{
System.out.print("Enter player's score: ");
score[i] = input.nextInt();
}
//Print name and score for each array.
for (int i = 0; i < numberOfPlayers; i++)
{
System.out.println("Player: " + name [i] + " , " + "score: " + score [i]);
}
}//Close Main
//method that will assort each score lowest to highest
public static void selectionSort(int[] list)
{
for (int i = 0; i < list.length - 1; i++)
{
int currentMin = list[i];
int currentMinIndex = i;
for (int j = i + 1; j < list.length; j++)
{
if (currentMin > list[j])
{
currentMin = list[j];
currentMinIndex = j;
}
}
if (currentMinIndex != i)
{
list[currentMinIndex] = list[i];
list[i] = currentMin;
}
}
}//Close method selectionSort
}//close Class
使用のSortedMapを...それはあなたの鍵を並べ替えるので、あなたでしょうキーと名前として値を設定し、すべての作業を行います。 –
もし彼らがどのような見た目で始まる同じ索引であれば...あなたの選択をしている間にスコア上で並べ替えると、名前でもやるべきです....名前や何でも比較しないでください – 3kings