Write a Java program to find the maximum and minimum value of an array. code example
Example 1: how to get the max value of an array java
public static double arrayMax(double[] arr) {
double max = Double.NEGATIVE_INFINITY;
for(double cur: arr)
max = Math.max(max, cur);
return max;
}
Example 2: finding min and max from given number in java
Scanner input = new Scanner(System.in);
int count = 0;
int min = 0;
int max = 0;
boolean bugSolved = true;
while (true){
int cnt = count++;
System.out.print("Enter Number #"+(cnt+1)+": ");
boolean isValid = input.hasNextInt();
if(isValid){
int num = input.nextInt();
if (num < min) {
min = num;
}else if (num > max){
max = num;
}
}else{
System.out.println("Invalid input..");
break;
}
input.nextLine();
}
System.out.println("Min Number : " + min);
System.out.println("Max Number : " + max);