Question: In this exercise, rewrite the for-each loop that finds the largest value in an array as a for or while loop. Below is the code
In this exercise, rewrite the for-each loop that finds the largest value in an array as a for or while loop.
Below is the code they wanted us to change:
public class LargestValue { public static void main(String[] arg) { { int[] values = {32, 56, 79, 2, 150, 37}; int highestValue = findMax(values); System.out.println("The highest score is " + highestValue); } } public static int findMax(int[] numbers) { int maxSoFar = numbers[0]; // for each loop to rewrite as for loop for (int i = 0; i < value.length; i++) { if (num> maxSoFar) { maxSoFar = num; } } return maxSoFar; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
