gpt4 book ai didi

java - 获取数据集并将其打印在二维数组中

转载 作者:太空宇宙 更新时间:2023-11-04 09:54:58 24 4
gpt4 key购买 nike

所以我已经将代码编写到一个程序中,该程序应该初始化来自不同位置的总计数据集并按原样显示它们。由于某种原因,我在第一个 for 循环中不断收到运行时错误,但无法弄清楚原因。有人可以帮我弄清楚我做错了什么吗?

public class Sales {

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;


/**
* @param args the command line arguments
*/
public static void main(String[] args) {

calCityTotal();
calMonthlyTotal();
displayTable();

}

public Sales() {

months = new String[] {"January","Febuary","March","April",
"May","June"};

cities = new String[] {"Chilliwack","Kamloops","Kelowna",
"NanaimoSurrey","Vancouver","Victoria"};

sales = new int[][] {{400,500,500,600,500,600},
{600,800,800,800,900,900},
{700,700,700,900,900,1000},
{500,600,700,800,700,700},
{900,900,900,1000,1100,1100}};

citySum = new int[sales.length];

monthlySum = new int[sales[0].length];
}

public static void calCityTotal() {

for (row = 0; row < sales.length; row++){
for (col = 0; col < sales[0].length; col++){
citySum[col] += sales[row][col];
}
}
}

public static void calMonthlyTotal() {

for (row = 0; row < sales.length; row++){
for (col = 0; col < sales[0].length; col++){
monthlySum[row] += sales[row][col];
}
}
}

最佳答案

您当前的代码有 2 个问题:

1) 变量未初始化。为此,您可以在 main 方法中创建 Sales 对象(从 calCityTotalcalMonthlyTotal 中删除 static 关键字)。

2) 解决上述问题后,您将遇到 ArrayindexoutofboundsException,因为 citySum 的长度为 sales.length,而 calCityTotal 中的循环转到 sales[0].length

将变量声明为静态并在构造函数中初始化它们是没有意义的。静态变量应该独立于任何实例。浏览Java: when to use static methods知道何时声明静态变量。如果要将变量声明为 static,则应在 static block 中初始化它们( Static Block in Java )。

下面的代码可以工作:

public class Sales {

private String[] months;
private String[] cities;
private int[] citySum;
private int[] monthlySum;
private int[][] sales;
private int col;
private int row;


/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Sales salesObj = new Sales();
salesObj.calCityTotal();
salesObj.calMonthlyTotal();

}

public Sales() {


months = new String[]{"January", "Febuary", "March", "April",
"May", "June"};

cities = new String[]{"Chilliwack", "Kamloops", "Kelowna",
"NanaimoSurrey", "Vancouver", "Victoria"};

sales = new int[][]{{400, 500, 500, 600, 500, 600},
{600, 800, 800, 800, 900, 900},
{700, 700, 700, 900, 900, 1000},
{500, 600, 700, 800, 700, 700},
{900, 900, 900, 1000, 1100, 1100}};

citySum = new int[sales.length+1];

monthlySum = new int[sales[0].length];
}

public void calCityTotal() {

for (row = 0; row < sales.length; row++) {
for (col = 0; col < sales[0].length; col++) {
citySum[col] += sales[row][col];
}
}
}

public void calMonthlyTotal() {

for (row = 0; row < sales.length; row++) {
for (col = 0; col < sales[0].length; col++) {
monthlySum[row] += sales[row][col];
}
}
}

}

关于java - 获取数据集并将其打印在二维数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54264540/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com