简体   繁体   English

计算此二维数组的平均值

[英]Calculate the average of this two dimensional array

What I'm now looking for is a way to print the average of second row of this code. 我现在正在寻找的是一种打印此代码第二行平均值的方法。

import java.util.Scanner;
public class MojaLoi{
public static void main(String[] args){
    Scanner sc = new Scanner(System.in);

    int [][] a = new int[6][6];

    for(int i=0; i<a.length; i++){
        for(int j=0; j<a[i].length; j++){
            a[i][j] = sc.nextInt();
        }
    }
    for(int i=0; i<a.length; i++){
        for(int j=0; j<a[i].length; j++){
            System.out.print(a[i][j]+" ");
        }
        System.out.println();
    }
}

Any reply greatly appreciated. 任何答复,不胜感激。

To calculate average of just the second row use: 要计算仅第二行的平均值,请使用:

float sum = 0;
for(int j=0; j<a[0].length; j++){
    sum += a[1][j];   // i = 1 for second row
}
float average = sum / a[0].length;
System.out.println(average);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM