第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

獲取數(shù)據(jù)集并將其打印到二維數(shù)組中

獲取數(shù)據(jù)集并將其打印到二維數(shù)組中

臨摹微笑 2022-05-12 17:35:35
所以我已經(jīng)將代碼編寫到一個(gè)程序中,該程序應(yīng)該初始化來自不同位置的總計(jì)數(shù)據(jù)集并顯示它們。出于某種原因,我在第一個(gè) for 循環(huán)中不斷收到運(yùn)行時(shí)錯(cuò)誤,無法弄清楚原因。有人可以幫我弄清楚我做錯(cuò)了什么嗎?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 回答

?
侃侃無極

TA貢獻(xiàn)2051條經(jīng)驗(yàn) 獲得超10個(gè)贊

您當(dāng)前的代碼有兩個(gè)問題:


1)變量沒有被初始化。為此,您可以Sales在方法中創(chuàng)建對(duì)象(從andmain中刪除靜態(tài)關(guān)鍵字)。calCityTotalcalMonthlyTotal


2)一旦上述問題得到解決,你會(huì)遇到Arrayindexoutofboundsexception因?yàn)閏itySum有長(zhǎng)度sales.length而你的循環(huán)calCityTotal進(jìn)入sales[0].length。


將變量聲明為static并在constructor. 靜態(tài)變量應(yīng)該獨(dú)立于任何實(shí)例。通讀Java:什么時(shí)候使用靜態(tài)方法才知道什么時(shí)候聲明靜態(tài)變量。如果要將變量聲明為static,則應(yīng)在static塊中初始化它們(Java 中的靜態(tài)塊)。


下面的代碼將起作用:


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];

        }

    }

}

}


查看完整回答
反對(duì) 回復(fù) 2022-05-12
?
翻過高山走不出你

TA貢獻(xiàn)1875條經(jīng)驗(yàn) 獲得超3個(gè)贊

你得到了NullPointerException因?yàn)閟ales在nullline for (row = 0; row < sales.length; row++)。


即使您sales在構(gòu)造函數(shù)中為變量設(shè)置了一個(gè)值Sales(),您也永遠(yuǎn)不會(huì)調(diào)用該構(gòu)造函數(shù)(如new Sales())。


因此,要解決此問題,NullPointerException您可以在方法中調(diào)用Sales構(gòu)造函數(shù),main()如下所示:


public static void main(String[] args)

{

  new Sales();


  calCityTotal();

  calMonthlyTotal();

  displayTable();

}

編輯

修復(fù)后NullPointerException,您的代碼仍然有一些其他問題。


在里面calCityTotal(),我認(rèn)為sales[0].length應(yīng)該更正。sales[row].length


citySum數(shù)組初始化為sales. 這意味著citySum.length等于“行”的數(shù)量。但是你寫citySum[col]了這會(huì)導(dǎo)致ArrayIndexOutOfBoundsException因?yàn)椤傲小钡臄?shù)量可以超過citySum.length. (這實(shí)際上發(fā)生在您的程序中。因?yàn)樾袛?shù) = 5,列數(shù) = 6。)


查看完整回答
反對(duì) 回復(fù) 2022-05-12
  • 2 回答
  • 0 關(guān)注
  • 110 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)