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

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

使用用戶輸入創(chuàng)建 2 維數(shù)組并查找特定列的總和

使用用戶輸入創(chuàng)建 2 維數(shù)組并查找特定列的總和

慕田峪9158850 2023-06-08 20:20:47
請協(xié)助我的代碼。數(shù)組中必須有 3 行和 4 列。用戶必須能夠為前 2 列輸入雙精度值。然后我應該能夠計算數(shù)組中每一列的總和。public class Main {    public static void main(String[] args)     {      // Implement scanner      Scanner input = new Scanner(System.in);      // Create loop for accepting matrix input      // Determine row size      System.out.println("Please enter the number 3 for the number of rows in the array:");      int row = input.nextInt();      //Rule for row not being 3      while (row != 3)      {      System.out.println("Sorry, there must be 3 rows.");      row = input.nextInt();      }      // Determine column size      System.out.println("Please enter the number 4 for the number of columns in the array:");      int column = input.nextInt();      //Rule for column not being 4      while (column != 4)      {      System.out.println("Sorry, there must be 4 columns.");      column = input.nextInt();      }      // Declare array with row and columns the user gave      int[][] userArray = new int[row][column];      //Informing user how data is inputted and saved      System.out.print("Note that the following inputs saves the numbers from left to right. So after entering 4 digits it moves onto the next row.");      System.out.println("\n");      for(int i=0;i < row ; i++)      {        for(int j=0; j< column; j++)        {         System.out.print("Please enter a value for the array:["+i+"]["+j+"]");        int val = input.nextInt();        userArray[i][j] = val;        }      }      printMatrix(userArray, row, column);    }    public static void printMatrix(int[][] array, int row, int column)    {      for (int i = 0; i < row; i++)      {        for (int j = 0; j < column; j++)        {          System.out.print(array[i][j] + " ");        }          System.out.println();      }    }}根據(jù)用戶輸入,打印輸出應該是:1.9 2.3 5 15.0 7.3 6 82.4 3.1 3 2第 1 列的總和為:9.3第 2 列的總和為:12.7第 3 列的總和為:14第 4 列的總和為:11
查看完整描述

2 回答

?
莫回無

TA貢獻1865條經(jīng)驗 獲得超7個贊

您現(xiàn)在需要按列添加數(shù)組值。它可以很簡單:


private void printSumForColumn(int[][] array, int col)

{

  int sum = 0;

  for (int i = 0; i < row; i++)

  {

    sum += array[i][col];

  }

  System.out.println("The sum of column " + col + " is " + sum);

}


查看完整回答
反對 回復 2023-06-08
?
慕神8447489

TA貢獻1780條經(jīng)驗 獲得超1個贊

首先,你不能使用 int 數(shù)組來存儲浮點數(shù)。所以printMatrix方法簽名應該改為double[][].


這是一種計算列總和的方法


public static void calColSum(double[][] matrix){


    // number of columns

    int col = matrix[0].length;


    // array to hold column sum

    double[] colSums = new double[col];


    for (double[] row : matrix) {

        for (int y = 0; y < col; y++) {

            colSums[y] += row[y];

        }

    }


    for(int x = 0; x< colSums.length; x++){

        System.out.println("The sum of column " + x +" is: " + colSums[x]);

    }

}


查看完整回答
反對 回復 2023-06-08
  • 2 回答
  • 0 關注
  • 134 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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