第8周-位置类 Location

问题及代码:

/*    
*Copyright (c)2015,烟台大学计算机与控制工程学院    
*All rights reserved.    
*文件名称:Location.java    
*作    者:单昕昕    
*完成日期:2015年10月21日    
*版 本 号:v1.0    
*问题描述:设计一个位置类。 
*程序输入:二维数组。 
*程序输出:数组中的最大值及其位置。 
*/
import java.util.*;
import java.util.Scanner;
class Location
{
    int row,column;
    double maxValue;
    public void setRow(int i)
    {
        row=i;
    }
    public void setColumn(int j)
    {
        column=j;
    }
    public int getRow()
    {
        return row;
    }
    public int getColumn()
    {
        return column;
    }
}

public class Test
{
    public static Location locateLargest(double [][]a)
    {
        Location l=new Location();
        l.maxValue=a[0][0];
        l.setRow(0);
        l.setColumn(0);
        int i,j;
        for(i=0; i<a.length; ++i)
            for(j=0; j<a[i].length; ++j)
                if(a[i][j]>l.maxValue)
                {
                    l.maxValue=a[i][j];
                    l.setRow(i);
                    l.setColumn(j);
                }
        return l;
    }
    public static void main(String[] args)
    {
        Test location1=new Test();
        Location location2=new Location();
        System.out.print("Enter the number of rows and columns of the array:");
        Scanner input=new Scanner(System.in);
        int m=input.nextInt();
        int n=input.nextInt();
        double [][]a=new double [m][n];
        System.out.println("Enter the array:");
        int i,j;
        for(i=0; i<m; ++i)
            for(j=0; j<n; ++j)
                a[i][j]=input.nextDouble();
        
        location2 =location1.locateLargest(a);
        System.out.println("The location of the largest element is "+location2.maxValue+" at ("+location2.getRow()+","+location2.getColumn()+")");
    }
}


//测试数据
/*
23.5 35 2 10
4.5 3 45 3.5
35 44 5.5 9.6
*/

运行结果:



知识点总结:

类和对象


学习心得:

因为一个地方的小Bug被坑了好久,唉真是跪了。。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值