求出二维数组中的最大元素值 和 求出它的两个下标,并求出一维数组的平均值。

package ShuZu;

import java.util.Arrays;
import java.util.Scanner;

/**
 * 功能说明:求出二维数组中的最大元素值 和 求出它的两个下标,并求出一维数组的平均值。
 * @author 夏大昌
 * @Date 2022/7/27 18:31
 */
public class ErWeiShuZu {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("输入数组的行数:");
        int h = input.nextInt();
        System.out.print("输入数组的列数:");
        int l = input.nextInt();
        int[][] a = new int[h][l];
        int j = 0;
        int sum=0;//和
        int avg=0;//平均值
        int th=0;//总长度
        for(int i = 0; i < a.length; i++) {
            System.out.println("输入第" + (i + 1) + "个数组:");
            for (j = 0; j < a[i].length; j++) {
                a[i][j] = input.nextInt();
                sum = sum + a[i][j];
            }
            th = th + a[i].length;
        }
        avg = sum / th;
        Location  location1=locateLargest(a);
        System.out.println("最大元素值: "+location1.maxValue+"\n下表是:"+"("+location1.row+" , "+location1.column+")"+"\n数组平均值:"+avg);
    }
    public static Location locateLargest(int[][] a) {
        Location location = new Location();
        int h = 0;
        int l = 0;
        double maxValue = 0;
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[i].length; j++) {
                if(a[i][j]>maxValue){
                    maxValue=a[i][j];
                    h =i;
                    l=j;
                }
            }
        }
        location.row = h;
        location.column = l;
        location.maxValue = maxValue;
        return location;
    }
}
class Location{
    public  int row;//下标
    public int column;//下标
    public  double maxValue;//最大值
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值