欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

Java:,

来源: javaer 分享于  点击 48873 次 点评:47

Java:,


设计一个名为Location的类,定位二维数组中的最大值及其位置。这个类包括公共的数据域row、column和maxValue,二维数组中的最大值及其下标用int型的row和column以及double型的maxValue存储。编写下面的方法,返回一个二维数组中最大值的位置。

public static Location locateLargest(double[][] a)

返回值是一个Location的实例。

贴代码


import java.util.Scanner;
public class Exercise8_13 {


    public static void main(String[] args) {

       System.out.println("Enter the number of rows and colums of the array :");
        Scanner input = new Scanner(System.in);
        int row = input.nextInt();
        int column = input.nextInt();
        System.out.println("Enter the array");
        double[][] array = new double[row][column];
        for(int i=0 ;i < array.length;i++){
            for(int j=0 ;j <array[i].length ;j++ ){
                array[i][j] = input.nextDouble();
            }
        }
        Location  location1=locateLargest(array);
        System.out.println("The location of the largets element is "+location1.maxValue+"at "+"("+location1.row+","+location1.column+")");
    }
    public static Location locateLargest(double[][] a) {
        Location location = new Location();
        int row = 0;
        int column = 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];
                    row =i;
                    column=j;
                }

            }

        }
        location.row = row;
        location.column = column;
        location.maxValue = maxValue;
        return location;//实例
}
}
class Location{
    public  int row;
    public int column;
    public  double maxValue;




    }

相关文章

    暂无相关文章
相关栏目:

用户点评