I need help with this code !! import java.util.Arrays; import java.util.Scanner; public class MaxElement { public static void main(String[] args) { //create an object for Scanner class Scanner x = new Scanner (System.in); System.out.print ("Enter 10 integers: "); // create an array Integer[] arr = new Integer[10]; // Execute for loop for (int i = 0; i < arr.length; i++) { //get the 10 integers arr[i] = x.nextInt(); } // Print the maximum number System.out.print("The max number is = " + max(arr)); System.out.print("\n"); } //max method public static > E max(E[] arr) { E max = arr[0]; // Execute for loop for (int i = 1; i < arr.length; i++) { E element = arr[i]; if (element.compareTo(max) > 0) { max = element; } } return max; } }
I need help with this code !!
import java.util.Arrays;
import java.util.Scanner;
public class MaxElement {
public static void main(String[] args)
{
//create an object for Scanner class
Scanner x = new Scanner (System.in);
System.out.print ("Enter 10 integers: ");
// create an array
Integer[] arr = new Integer[10];
// Execute for loop
for (int i = 0; i < arr.length; i++) {
//get the 10 integers
arr[i] = x.nextInt();
}
// Print the maximum number
System.out.print("The max number is = " + max(arr));
System.out.print("\n");
}
//max method
public static <E extends Comparable<E>> E max(E[] arr)
{
E max = arr[0];
// Execute for loop
for (int i = 1; i < arr.length; i++) {
E element = arr[i];
if (element.compareTo(max) > 0) {
max = element;
}
}
return max;
}
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images