java - 使用文件初始化二维数组
问题描述
我很难弄清楚我应该做什么。基本上,我应该获取一个包含城市名称列表以及每个城市名称列表的文件。我已经创建了一个可以使用预初始化列表的程序,但是现在我的教授要求我们对其进行修改以接收文件...如果有人可以提供帮助,我将不胜感激!
package assignment7;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
/*******************************************************************************
* The program first initializes the arrays using a constructor method and
* then calculates the total sales for each city and month and prints the
* formatted table out to the user.
*******************************************************************************/
public class Assignment7 {
private static String[] months;
private static String[] cities;
private static int[] citySum;
private static int[] monthlySum;
private static int[][] sales;
private static int col;
private static int row;
public static void main(String[] args) {
Assignment7 sale = new Assignment7();
sale.calCityTotal();
sale.calMonthlyTotal();
sale.displayTable();
}
/***********************************************************
* Constructor method for Sales.java
***********************************************************/
public Assignment7() {
File file = new File("Sales.txt");
try {
PrintWriter output = new PrintWriter(file);
output.print("7 6\n" +
"Chilliwack\n" +
"Kamloops\n" +
"Kelowna\n" +
"Nanaimo\n" +
"Surrey\n" +
"Vancouver\n" +
"Victoria\n" +
"400 500 500 600 500 600 \n" +
"600 800 800 800 900 900 \n" +
"700 700 700 900 900 1000 \n" +
"500 600 700 800 700 700 \n" +
"800 700 800 700 900 800 \n" +
"1000 1100 1200 1300 1400 1400 \n" +
"900 900 900 1000 1100 1100 ");
output.close();
Scanner scan = new Scanner(file);
months = new String[6];
cities = new String[7];
sales = new int[7][6];
citySum = new int[sales.length];
monthlySum = new int[sales.length];
}
catch (IOException exception) {
System.out.println("File error.");
}
}
/***********************************************************
* This method calculates the total sales for each city
* and stores the values in an array.
***********************************************************/
public void calCityTotal() {
for (row = 0; row < sales.length; row++) {
for (col = 0; col < sales[0].length; col++) {
citySum[row] += sales[row][col];
}
}
}
/***********************************************************
* This method calculates the total sales for each month and
* stores the values in an array.
***********************************************************/
public void calMonthlyTotal() {
for (row = 0; row < sales.length; row++) {
for (col = 0; col < sales[0].length; col++) {
monthlySum[col] += sales[row][col];
}
}
for (int row = 0; row < citySum.length; row++) {
monthlySum[6] += citySum[row];
}
}
/***********************************************************
* This method creates a formatted version of a table so the
* user will easily be able to see the data and displays it
* to the user.
***********************************************************/
public void displayTable() {
System.out.println("Store\t\t\t Monthly Sales\t\t\t\tStore");
System.out.println("Location\t\t (thousands of $)\t\t\tTotals");
System.out.print("\t\t");
for (int col = 0; col < sales[0].length; col++) {
System.out.print(months[col]);
System.out.print("\t");
}
for (int row = 0; row < sales.length; row++) {
System.out.print("\n" + cities[row]);
for (int col = 0; col < sales[0].length; col++) {
System.out.print("\t" + sales[row][col]);
}
System.out.print("\t" + citySum[row]);
}
System.out.print("\nMonth totals\t");
for (int row = 0; row < sales.length; row++) {
System.out.print(monthlySum[row] + "\t");
}
}
}
解决方案
推荐阅读
- java - 将颜色设置为红色以在 jtextarea 上显示的字符串变量活动
- reactjs - 使用 Rails 布局从其他反应组件渲染反应组件
- matlab - MATLAB,即使我在 imresize 中使用了错误的代码,但它会被执行
- bash - 将一个文件中的数字替换为另一个文件中的数字
- javascript - 如何将字符串转换为 json 对象
- android - RecyclerView 的 SearchView 不起作用
- javascript - WARNING in Invalid background value at Ignoring。在角
- qt - 迭代 ButtonGroup QML 中的按钮
- android - 何时使用 System.exit(0) 和 System.exit(2)?
- php - phpMyAdmin 生成随机 CSRF 令牌失败!Windows 上的 phpMyAdmin 4.6.6