Write a method that takes a one-dimensional array of doubles as a parameter, and returns the largest value in the array.
时间: 2024-03-26 09:34:47 浏览: 52
c#求两个数中最大值的方法
Sure, here's the code for a method that takes a one-dimensional array of doubles as a parameter and returns the largest value in the array:
```
public static double findLargest(double[] arr) {
double largest = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > largest) {
largest = arr[i];
}
}
return largest;
}
```
This method initializes a variable `largest` to the first element of the input array `arr`. It then iterates through the remaining elements of the array, comparing each element to the current largest value. If it finds an element larger than the current largest value, it updates the `largest` variable to that value. Finally, it returns the largest value found in the array.
阅读全文