【发布时间】:2015-10-17 18:47:10
【问题描述】:
import java.util.Scanner;
public class scores
{
static Scanner input = new Scanner(System.in);
public static void main(String[] args)
{
System.out.print("\f");
int classSize, counterScore, counterName;
String name;
double score,average, sum;
System.out.print("Enter size of class: ");
classSize = input.nextInt();
int[] scoreArray = new int[classSize];
String[] nameArray = new String[classSize];
counterScore=1;
counterName = 1;
average = 0;
sum = 0;
for (int x = 0; x < classSize; x++)
{
input.nextLine();
System.out.print("Student " + counterName++ + " Name: ");
nameArray[x] = input.nextLine();
System.out.print("Student " + counterScore++ + " Score: ");
scoreArray[x] = input.nextInt();
sum = sum + scoreArray[x];
average = sum / classSize;
}
System.out.println(average);
}
}
我必须制作一个应用程序,让我知道有多少人参加了考试,然后输入他们的姓名和分数。我使用了两个不同的数组,一个是字符串,一个是双精度数组。我的输出旨在读取哪些名称低于平均值并显示名称。我不知道如何组合这两个数组,以便它识别出这个分数与这个名字有关,所以显示那个名字。
【问题讨论】:
-
创建一个由名字和分数组成的类
Person!?然后只创建一个 Person 数组 -
你已经很清楚了。您不需要实际组合 2 个数组,只需意识到 2 个数组中的相同索引对应于同一个人。所以nameArray[4]和scoreArray[4]对应的是同一个人。
-
或者使用
Map<String, Integer>,其中键是名称,值是分数。但是,如果您以后想要添加第三个信息,这将不太可扩展。