Find The Maximum Value in a List
Write a method that returns the largest integer in the list.
You can assume that the list has at least one element.
You can assume that the list has at least one element.
public class BestOnlineLearning { public static int maxNumber(int[] n){ int max = n[0]; for(int i=0; i<n.length; i++){ if(n[i] > max){ max = n[i]; } } System.out.println(max); return max; } public static void main(String[] args) { int[] n = {2,4,5,6,7,8,9,0,1,3}; maxNumber(n); } }
(OR)
you can also use the for-each
for(int val : n){ if(val > max){ max = val; } }
Leave a Comment