0
我在Java
中使用BlueJ
,我对它很陌生。如果有一件事我不擅长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”文件:
Krook 45
Leyer 40
Krook 76
Leyer 55
Krook 20
Krook 50
Leyer 28
Krook 30
Leyer 23
Krook 72
Krook 42
Krook 81
Leyer 64
Krook 52
Leyer 18
Leyer 34
Krook 60
Krook 26
Leyer 49
Krook 37
我必须使用这个模板:
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.
}
}
毕竟,我必须创建一个创建Table对象的主类,来自PROG4IN.TXT的数据,并打印报告。
我希望有人能帮助我。
预先感谢您。