[英]high scores java with arraylist
我正在編寫以下高分游戲,使用 ArrayLists 和 void 方法。 我在處理所需的輸出時遇到問題,它應該類似於以下內容:
Top Scorers:
Name1: 600
Name2: 400
Name3: 300
Name4: 200
Name5: 100
我遇到的復雜部分是在分數按降序排序后輸入的名稱與輸入的分數匹配。 以下是我到目前為止的代碼:
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"
方法令人困惑,這就是“sort”和“display”相似的原因。 這些是我到目前為止采取的步驟:
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_names
的索引和array_scores
排序后的array_scores
。 任何幫助都會非常有幫助。
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));
您可以使用自定義比較器對這個列表進行排序。
希望這可以幫助。
如果您不能使用比較器,則需要一些解決方法。 首先,您需要一個地圖,其中包含名稱的分數和值列表
Map<Integer,List<String>> map = new HashMap<Integer,List<String>>;
你在排序之前填寫這張地圖
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));
}
對分數列表進行排序后,您只需打印每個條目的映射列表
for(int i=0;i<scores;i++)
printList(map.get(scores.get(i));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.