获取数据集并将其打印到二维数组中

所以我已经将代码编写到一个程序中,该程序应该初始化来自不同位置的总计数据集并显示它们。出于某种原因,我在第一个 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];

        }

    }

}


临摹微笑
浏览 84回答 2
2回答

侃侃无极

您当前的代码有两个问题:1)变量没有被初始化。为此,您可以Sales在方法中创建对象(从andmain中删除静态关键字)。calCityTotalcalMonthlyTotal2)一旦上述问题得到解决,你会遇到Arrayindexoutofboundsexception因为citySum有长度sales.length而你的循环calCityTotal进入sales[0].length。将变量声明为static并在constructor. 静态变量应该独立于任何实例。通读Java:什么时候使用静态方法才知道什么时候声明静态变量。如果要将变量声明为static,则应在static块中初始化它们(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;/**&nbsp;* @param args the command line arguments&nbsp;*/public static void main(String[] args) {&nbsp; &nbsp; Sales salesObj = new Sales();&nbsp; &nbsp; salesObj.calCityTotal();&nbsp; &nbsp; salesObj.calMonthlyTotal();}public Sales() {&nbsp; &nbsp; months = new String[]{"January", "Febuary", "March", "April",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "May", "June"};&nbsp; &nbsp; cities = new String[]{"Chilliwack", "Kamloops", "Kelowna",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "NanaimoSurrey", "Vancouver", "Victoria"};&nbsp; &nbsp; sales = new int[][]{{400, 500, 500, 600, 500, 600},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {600, 800, 800, 800, 900, 900},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {700, 700, 700, 900, 900, 1000},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {500, 600, 700, 800, 700, 700},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {900, 900, 900, 1000, 1100, 1100}};&nbsp; &nbsp; citySum = new int[sales.length+1];&nbsp; &nbsp; monthlySum = new int[sales[0].length];}public void calCityTotal() {&nbsp; &nbsp; for (row = 0; row < sales.length; row++) {&nbsp; &nbsp; &nbsp; &nbsp; for (col = 0; col < sales[0].length; col++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; citySum[col] += sales[row][col];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}public void calMonthlyTotal() {&nbsp; &nbsp; for (row = 0; row < sales.length; row++) {&nbsp; &nbsp; &nbsp; &nbsp; for (col = 0; col < sales[0].length; col++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; monthlySum[row] += sales[row][col];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}}

翻过高山走不出你

你得到了NullPointerException因为sales在nullline for (row = 0; row < sales.length; row++)。即使您sales在构造函数中为变量设置了一个值Sales(),您也永远不会调用该构造函数(如new Sales())。因此,要解决此问题,NullPointerException您可以在方法中调用Sales构造函数,main()如下所示:public static void main(String[] args){&nbsp; new Sales();&nbsp; calCityTotal();&nbsp; calMonthlyTotal();&nbsp; displayTable();}编辑修复后NullPointerException,您的代码仍然有一些其他问题。在里面calCityTotal(),我认为sales[0].length应该更正。sales[row].lengthcitySum数组初始化为sales. 这意味着citySum.length等于“行”的数量。但是你写citySum[col]了这会导致ArrayIndexOutOfBoundsException因为“列”的数量可以超过citySum.length. (这实际上发生在您的程序中。因为行数 = 5,列数 = 6。)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java