Java 二维阵列问题。希望得到帮助
我在Java 二维阵列问题。希望得到帮助,java,arrays,Java,Arrays,我在BlueJ中使用Java,我对它相当陌生。如果有一件事我在Java中不太擅长的话,那就是数组,更不用说二维数组了。希望有人能在这个项目上帮助我,因为我感到有些不知所措,甚至不知道从哪里开始 这个程序的目的是使用一个二维表格从一个.TXT文件中统计政治投票的结果。 输入文件“PROG4IN.TXT”为每个轮询的投票者包含一行。每一行都包含受欢迎候选人的姓名和选民的年龄。 使用一个两行三列的数组,我必须按支持的候选人和年龄组对选民进行统计;这三个年龄组分别为18-29岁、30-49岁和50-99
BlueJ
中使用Java
,我对它相当陌生。如果有一件事我在Java中不太擅长的话,那就是数组,更不用说二维数组了。希望有人能在这个项目上帮助我,因为我感到有些不知所措,甚至不知道从哪里开始
这个程序的目的是使用一个二维表格从一个.TXT文件中统计政治投票的结果。
输入文件“PROG4IN.TXT”为每个轮询的投票者包含一行。每一行都包含受欢迎候选人的姓名和选民的年龄。
使用一个两行三列的数组,我必须按支持的候选人和年龄组对选民进行统计;这三个年龄组分别为18-29岁、30-49岁和50-99岁
这就是期望的最终输出的样子:
Candidate 18-29 30-49 50-99 Total
Krook 2 4 6 12
Leyer 3 3 2 8
以下是“PROG4IN.TXT”文件中的内容,仅供参考:
我必须使用此模板:
public class Table {
private int[][] table;
private String[] names;
public Table() {
// Create the two-dimensional tally array "table"
// having 2 rows and 3 columns. Row 0 corresponds
// to candidate Krook and row 1 to candidate Leyer.
// The columns correspond to the three age groups
// 18-29, 30-49, and 50-99. Initialize all the
// tallies to zero. Create the array "names" to
// hold the candidate names: names[0]="Krook" and
// names[1]="Leyer".
}
public void tally(String name, int age) {
// Add one to the tally in the "table" array that
// corresponds to the name and age passed as arguments.
// Hint: Use the equals method to determine whether
// two strings are equal: name.equals("Krook") is
// true when name is "Krook".
}
public void report() {
// Use nested loops to print a report in the format
// shown above. Assume that the tallies have already
// been made.
}
}
然而,在所有这些之后,我必须创建一个主类来创建一个表对象,计算PROG4IN.TXT中的数据,并打印报告
我希望有人能在这方面帮助我
提前感谢您。您的阵列将如下所示:
table = new int[2][3]();
{
{count for 1st age group for krook, for 2nd age group for krook, last group for krook}
{count for 1st age group for leyer, for 2nd age group for leyer, last group for leyer}
}
里面会是这样的:
table = new int[2][3]();
{
{count for 1st age group for krook, for 2nd age group for krook, last group for krook}
{count for 1st age group for leyer, for 2nd age group for leyer, last group for leyer}
}
例如,当你想为leyer加入中年组时,你可以做表[1][1]+=一些金额
对于krook的第三个年龄组,您需要执行表[0][2]+=someamount