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

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用Java分別對二維數(shù)組的行和列求和?

如何使用Java分別對二維數(shù)組的行和列求和?

猛跑小豬 2021-06-02 17:50:37
這是我正在解決的問題:編寫一個程序,讀取一個 3 x 4 矩陣并分別顯示每列和每行的總和。這是示例運(yùn)行:逐行輸入 3×4 矩陣:1.5 2 3 45.5 6 7 89.5 1 3 1Sum of the elements at column 0 is 16.5Sum of the elements at column 1 is 9.0Sum of the elements at column 2 is 13.0Sum of the elements at column 3 is 13.0Sum of the elements at Row 0 is: 10.5Sum of the elements at Row 0 is: 26.5Sum of the elements at Row 0 is: 14.5這是我想出的代碼:package multidimensionalarrays;public class MultidimensionalArrays {    public static void main(String[] args) {        double sumOfRow = 0;        double[][] matrix = new double[3][4];        java.util.Scanner input = new java.util.Scanner(System.in); //Scanner        System.out.println("Enter a 3 by 4 matrix row by row: ");        //Prompt user to enter matrix numbers        for (int row = 0; row < matrix.length; row++) {            for (int col = 0; col < matrix[0].length; col++) {                matrix[row][col] = input.nextDouble();            }        }        double[] sumOfCol =new double[matrix[0].length];          for (int i = 0; i < matrix.length; i++) { //row            for (int j = 0; j < matrix[i].length; j++) { //column                sumOfRow += matrix[i][j];                sumOfCol[j] += matrix[i][j];            }            System.out.println("Sum of the elements at row " + row + " is: " + sumOfRow);        }        System.out.println("Sum of the elements at column " + col + " is: " + sumOfCol);    }             }   我的問題是,當(dāng)最后打印列和行的總和時,它無法識別row或col變量。我一直在玩它,現(xiàn)在可能已經(jīng)改變了幾個小時,但我似乎無法做到這一點,有人可以幫助我解決我做錯了什么嗎?另外我不知道我是否正確地進(jìn)行了列求和?
查看完整描述

2 回答

?
DIEA

TA貢獻(xiàn)1820條經(jīng)驗 獲得超2個贊

計算行數(shù)的變量i在您打印行總和時調(diào)用。

對于列總和,您需要另一個循環(huán)來一個一個地打印它們。


查看完整回答
反對 回復(fù) 2021-06-17
  • 2 回答
  • 0 關(guān)注
  • 789 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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