Java黑皮书课后题第9章:**9.13(Location类)设计一个名为Location的类,定位二维数组中的最大值及其位置。
生活随笔
收集整理的這篇文章主要介紹了
Java黑皮书课后题第9章:**9.13(Location类)设计一个名为Location的类,定位二维数组中的最大值及其位置。
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Java黑皮書課后題第9章:**9.13(Location類)設計一個名為Location的類,定位二維數組中的最大值及其位置
- 題目
- 破題
- 代碼
- Test13
- Test13_Location
- 運行結果
題目
破題
Test13:測試程序
Test13_Location:實現題目要求
代碼
Test13
import java.util.Scanner;public class Test13 {public static void main(String[] args) {// 定義數組Scanner input = new Scanner(System.in);System.out.print("Enter the number of rows and columns in the array:");int rows = input.nextInt(), cols = input.nextInt();double[][] arr = new double[rows][cols];// 數組賦值System.out.println("Enter the array:");for (int a = 0 ; a < rows ; a++){for (int b = 0 ; b < cols ; b++){arr[a][b] = input.nextDouble();}}// 調用找最大值方法Test13_Location l = new Test13_Location();l = locateLargest(arr);// 輸出結果System.out.println("The location of the largest elements is " + Test13_Location.maxValue +" at (" + Test13_Location.row + ", " + Test13_Location.column + ")");}public static Test13_Location locateLargest(double[][] a){Test13_Location l = new Test13_Location();for (int m = 0 ; m < a.length ; m++){for (int n = 0 ; n < a[0].length ; n++){if (Test13_Location.maxValue < a[m][n]){Test13_Location.maxValue = a[m][n];Test13_Location.row = m;Test13_Location.column = n;}}}return l;} }Test13_Location
public class Test13_Location {public static int row = 0, column = 0;public static double maxValue = 0.0; }運行結果
Enter the number of rows and columns in the array:3 4 Enter the array: 23.5 35 2 10 4.5 3 45 3.5 35 44 5.5 9.6 The location of the largest elements is 45.0 at (1, 2)總結
以上是生活随笔為你收集整理的Java黑皮书课后题第9章:**9.13(Location类)设计一个名为Location的类,定位二维数组中的最大值及其位置。的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java黑皮书课后题第9章:**9.12
- 下一篇: Java黑皮书课后题第10章:*10.1