2016-10-06 5 views
-1

私は、ArrayListsとvoidメソッドを使って、以下の高得点ゲームを書いています。イムは、次のようになります。必要な出力、とのトラブルを抱えて:ハイスコアjava with arraylist

Top Scorers: 
Name1: 600 
Name2: 400 
Name3: 300 
Name4: 200 
Name5: 100 

私はスコアAFTER inputedスコアとのマッチングinputed名前が降順にソートされていているとのトラブルを抱えています複雑な部分。以下は、私がこれまで持っているコードです:私たちの現在のレベルに基づいて

import java.util.Scanner; 
import java.lang.reflect.Array; 
import java.util.ArrayList; 
import java.util.Arrays; 



public class high_scores { 

//Write a program that records high-score data for a fictitious game. 
    //The program will ask the user to enter five names, and five scores. 
    //It will store the data in memory, and print it back out sorted by score. 





    //array lists declared and initialized in main method; invoke other three methods 
    public static void main(String args []) 
    { 

     //creating an arraylist with names 
     ArrayList<String> names = new ArrayList<String>(); 

     //creating an arraylist with scores 
     ArrayList<Integer> scores = new ArrayList<Integer>(); 


     //invoke other three methods 
     initialize(names, scores); 
     sort(names, scores); 
     display(names, scores); 

    } 



    //user input of five names and five scores; 
    public static void initialize(ArrayList<String> names, ArrayList<Integer> scores) 
    { 

     //for user input 
     Scanner keyboard = new Scanner(System.in); 

     System.out.print("Enter the name for score # 1: "); 
     names.add(keyboard.next()); 
     System.out.print("Enter the score for score # 1: "); 
     scores.add(keyboard.nextInt()); 

     System.out.print("Enter the name for score # 2: "); 
     names.add(keyboard.next()); 
     System.out.print("Enter the score for score # 2: "); 
     scores.add(keyboard.nextInt()); 

     System.out.print("Enter the name for score # 3: "); 
     names.add(keyboard.next()); 
     System.out.print("Enter the score for score # 3: "); 
     scores.add(keyboard.nextInt()); 

     System.out.print("Enter the name for score # 4: "); 
     names.add(keyboard.next()); 
     System.out.print("Enter the score for score # 4: "); 
     scores.add(keyboard.nextInt()); 

     System.out.print("Enter the name for score # 5: "); 
     names.add(keyboard.next()); 
     System.out.print("Enter the score for score # 5: "); 
     scores.add(keyboard.nextInt()); 


    } 




    //function sorts both array lists based on values in scores array list 
    public static void sort(ArrayList<String> names, ArrayList<Integer> scores) 
    { 


      String array_names[] = new String[names.size()]; 
      for(int x = 0; x < names.size(); x++) { 
       array_names[x] = names.get(x); 
      } 


      Integer array_scores[] = new Integer[scores.size()]; 
      for(int y = 0; y < scores.size(); y++) { 
       array_scores[y] = scores.get(y); 
      } 


    } 


    //method that displays the contents of the two arraylists 
    public static void display(ArrayList<String> names, ArrayList<Integer> scores) 
    { 



     String array_names[] = new String[names.size()]; 
     for(int x = 0; x < names.size(); x++) { 
      array_names[x] = names.get(x);  
     } 


     Integer array_scores[] = new Integer[scores.size()]; 
     for(int y = 0; y < scores.size(); y++) { 
      array_scores[y] = scores.get(y); 

     } 


      Arrays.sort(array_scores); 


     System.out.println(array_names[4] + " : " + array_scores[4]); 
     System.out.println(array_names[3] + " : " + array_scores[3]); 
     System.out.println(array_names[2] + " : " + array_scores[2]); 
     System.out.println(array_names[1] + " : " + array_scores[1]); 
     System.out.println(array_names[0] + " : " + array_scores[0]); 


    } 
} 

、私はおそらくcompareTo, Collections, or Array.sort Descending.わからないように...与えられた方法を変更することができない、含まれてはならないものがあります。これまでのところ、最初の "void initialize"メソッドと"void display"メソッドを理解することしかできませんでした。"void sort"メソッドが混乱しています。これは"ソート "と"表示 "が似ている理由です。 。この時点:私は私が降順にinputedスコアを印刷するために管理しているほとんどの問題を抱えていますが、私はちょうどインデックスのarray_namesのとarray_scores AFTERと一致するように見えるカントどこ

1.) Initialize method: Array List user input 
2.) Sort method: Converts Array List to Array; grabs Array List data 
3.) Display method: Similar to sort; still working on display 

"void display"方法がありますarray_scoresをソートしました。どんな助けでも非常に役に立ちます。

+3

ヒント:クラスを作成し、theyr indexで接続された2つの 'List' beeingに依存しないでください。 – SomeJavaGuy

+1

2つのリストを使用している場合は、1つのリストに基づいてそれらを同時にソートする必要があります。 – progyammer

答えて

0
public class Score { 
    private String name = null; 
    private int score = 0; 

    public Score(String name, int score) { 
     this.name = name; 
     this.score = score; 
    } 
} 

あなたは、コレクション

List<Score> allScores = new ArrayList<Score>(); 

ため、このクラスを使用して、次の方法であなたの入力を追加することができます。

allScores.add(new Score("Name1",600)); 

をそして、あなたはカスタムコンパレータを使用して、このリストを並べ替えることができます。

これが役に立ちます。

+0

@Tom指摘してくれてありがとう。すみません、私の間違いです。 – Sanjeev

0

コンパレータを使用できない場合は、回避策が必要です。 まず、あなたはスコアをソートした後、この

 for(int i=0;i<scores;i++) { 

      if(map.get(scores.get(i)) == null) 
       map.put(scores.get(i),new ArrayList<String>()); 
      map.get(scores.get(i)).add(names.get(i)); 
     } 

sortinglike前にこのマップを埋める重要なスコアと名前

Map<Integer,List<String>> map = new HashMap<Integer,List<String>>; 

の値リストとマップを必要とするが、あなたが必要とするすべてがマップを印刷するリスト各エントリのリスト

 for(int i=0;i<scores;i++) 
      printList(map.get(scores.get(i));