【发布时间】:2014-09-25 04:34:41
【问题描述】:
import java.util.ArrayList;
import java.util.Scanner;
public class Assignment5
{
public static void initialize(ArrayList<String> names, ArrayList<Integer> scores)
{
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner (System.in);
// Ask the user for the names and scores for 5 people.
System.out.println("Enter the name for score #1: ");
names.add(keyboard.next());
System.out.println("Enter the score for score #1: ");
scores.add(keyboard.nextInt());
System.out.println("Enter the name for score #2: ");
names.add(keyboard.next());
System.out.println("Enter the score for score #2: ");
scores.add(keyboard.nextInt());
System.out.println("Enter the name for score #3: ");
names.add(keyboard.next());
System.out.println("Enter the score for score #3: ");
scores.add(keyboard.nextInt());
System.out.println("Enter the name for score #4: ");
names.add(keyboard.next());
System.out.println("Enter the score for score #4: ");
scores.add(keyboard.nextInt());
System.out.println("Enter the name for score #5: ");
names.add(keyboard.next());
System.out.println("Enter the score for score #5: ");
scores.add(keyboard.nextInt());
}
public static void sort(ArrayList<String> names, ArrayList<Integer> scores)
{
}
public static void display(ArrayList<String> names, ArrayList<Integer> scores)
{
for (int index = 0; index < names.size(); index++)
{
String userNames = names.get(index);
System.out.println(names.get(index));
}
for (int index = 0; index < scores.size(); index++)
{
Integer userScores = scores.get(index);
System.out.println(scores.get(index));
}
}
public static void main(String[] args)
{
// Create an ArrayList for names and scores.
ArrayList<String> names = new ArrayList<String>();
ArrayList<Integer> scores = new ArrayList<Integer>();
// Call the three methods.
initialize(names, scores);
sort(names, scores);
display(names, scores);
}
}
编写一个程序,记录一个虚构游戏的高分数据。该程序将要求用户输入五个姓名和五个分数。它将数据存储在内存中,并按分数排序打印出来。
您的程序的输出应该大致如下:
输入分数 #1 的名称:Suzy
输入分数#1的分数:600
输入乐谱 #2 的名称:Kim
输入分数 #2 的分数:9900
输入分数 #3 的名称:Bob
输入分数#3的分数:1012
输入乐谱 #4 的名称:Armando
输入分数 #4 的分数:8000
输入分数 #5 的名称:Tim
输入分数#5的分数:514
最佳射手:
金:9900
阿曼多:8000
鲍勃:1012
苏西:600
蒂姆:514
要求
数据必须存储在两个 ArrayList 中:一个名为名称的字符串 ArrayList 和一个名为 score 的整数 ArrayList。这些 ArrayList 必须在 main 方法中声明。
所有用户输入都应该在一个名为 InitializeArrays 的方法中完成。它应该具有以下签名:
public static void InitializeArrays(ArrayList names, ArrayList score)
您应该编写一个函数,根据分数数组列表中的值对两个数组列表进行排序。这是作业中在概念上更具挑战性的部分之一。您想要对分数数组列表进行排序,同时更改名称数组列表,以便名称继续按索引与其各自的分数对齐。在上面的示例数据中,当分数 9900 移动到分数数组列表的第一个元素时,名称“Kim”也应该移动到名称数组列表的顶部。该函数应具有以下签名:
public static void sort(ArrayList names, ArrayList score)
最后,您应该编写一个显示两个数组列表内容的方法。它应该具有以下签名:
public static void display(ArrayList 名称,ArrayList 分数)
main 方法应该很短。它应该只声明和初始化两个数组列表,然后调用这三个方法。
.......到目前为止,我只创建了两个数组列表,可以存储名称和分数。我不确定如何正确排序和显示它们。我注意到人们已经为类似情况创建了比较器,但我们还没有在课堂上介绍过。
【问题讨论】:
-
你有什么问题?
-
我现在正在尝试扩展该程序。有一种输入数据的方法,但我还没有弄清楚如何正确排序或显示任何内容。
-
好的,你的问题是什么?
-
你必须更加具体。
-
我对Java的那部分不熟悉,你能解释一下吗?