<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<!-- 2、设计一个名为Location的类,定位二维数组中的最大值及其位置。这个类包括属性row(行下标)、column(列下标)和maxValue(最大值)。编写下面的函数,返回一个Location的对象(对象中包含了二维数组的最大值、最大值的行下标和列下标): -->
<script>
class Location{
constructor(row,column,maxValue){
this._row=row
this._column=column
this._maxValue=maxValue
}
get row(){
return this._row
}
set row(val){
this._row=val
}
get column(){
return this._column
}
set column(val){
this._column=val
}
get maxValue(){
return this._maxValue
}
set maxValue(val){
this._maxValue=val
}
displiy(){
console.log("数组最大值"+this._maxValue)
console.log("最大值的行下标"+this._row)
console.log("最大值的列下标"+this._column)
}
}
//函数的功能:找出数组的最大值 用locate类的对象保存最大值和下标
//函数的参数是数组
//函数的返回值:location的对象
function LocateLargest(arr){
//1.创建Location类的对象:假设二维数组的第一个元素为最大值
let max_location =new Location(0,0,arr[0][0])
//2.遍历二维数组元素找出最大值
for(let i =0;i<arr.length;i++){ //外循环是数组的行下标
for(let j=0; j<arr[i].length;j++){ //内循环是数组的列下标
if(max_location.maxValue<arr[i][j]){
max_location.maxValue=arr[i][j]
max_location.row=i
max_location.column=j
}
}
}
//3.返回Location类的对象
return max_location
}
let arr = [[12,35,45,66],[78,95,12,65],[55,44,102,89]]
let locat = LocateLargest(arr)
locat.displiy()
</script>
</body>
</html>
设计一个名为Location的类,定位二维数组中的最大值及其位置。这个类包括属性row(行下标)、column(列下标)和maxValue(最大值)。编写下面的函数,返回一个Location的对象(对
最新推荐文章于 2022-11-24 17:56:43 发布