Java语言程序设计(基础篇) 第九章——9.13

Com.java

//初学
import java.util.Scanner;

public class Com{
	public static void main(String[] args) {
		// 9.13
		System.out.print("Enter the number of rows and columns in the array:");
		//定义输入
		Scanner inputRow = new Scanner(System.in);
		Scanner inputColumn = new Scanner(System.in);
		Scanner inputValue = new Scanner(System.in);
		//输入数组大小
		//行和列之间要使用‘Enter’
		int row = inputRow.nextInt();
		int column = inputColumn.nextInt();
		System.out.print("Enter the array:\n");
		//建立二维数组
		double[][] array = new double[row][column];	
		for(int i = 0; i < array.length; i++) {
			for(int j = 0; j < array[1].length; j++) {
				array[i][j] = inputValue.nextDouble();
			}
		}
		inputValue.close();
	
		//调用静态方法
		Location l = Location.locateLargest(array);
		
		System.out.print("The location of the largest element is " + l.maxValue + " at ");
		System.out.print("(" + l.row + "," + l.column +")" );
	}
}

Location.java

public class Location {
	//横、纵坐标及最大值的初始化
	public int row;
	public int column;
	public double maxValue;
	
	Location(){
	}

	public static Location locateLargest (double[][] a) {
		//查找最大数值的横纵坐标,并返回坐标值
		//静态方法
		Location l = new Location();
		l.maxValue = a[1][1]; // 初始化最大值为(1,1)处
		l.row = 1;
		l.column = 1;
		for(int i = 0; i < a.length; i++) {
			for(int j = 0; j < a[1].length; j++) {
				if( l.maxValue < a[i][j]) {
					l.maxValue = a[i][j];
					l.row = i + 1;
					l.column = j + 1;
				}
				else
					continue;
			}
		}
		return l;
	}
}

注:数组是0开始,但是坐标我还是习惯从1开始,所以对row和column分别加1。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值