I just want to add a simple where I can add an input line part by user instead of hard code defining an array. So I can just enter a couple number as a part of an array.
Here is my original code:
public class Test {
public static void main(String[] args) {
System.out.println("Largest in given array is " + max());
}
static int array[] = {10, 324, 45, 90, 9808};
// Method to find maximum in arr[]
public static int max() {
int i;
// Initialize maximum element
int max = array[0];
// Traverse array elements from second and
// compare every element with current max
for (i = 1; i < array.length; i++)
if (array[i] > max)
max = array[i];
return max;
}
}
My logic is this, tell me if I'm right or not
I need to add import java.util.Scanner; and then enter Scanner input = new Scanner(System.in);
But next part confuses me, should I change the max() into a string?
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
System.out.println("Largest in given array is " + max(int[] array));
}
Scanner input = new Scanner(System.in);
static int array[] = {10, 324, 45, 90, 9808};
// Method to find maximum in arr[]
public static int max(int[] array) {
int x;
// Initialize maximum element
int max = array[0];
// Traverse array elements from second and
// compare every element with current max
for (x = 1; x < array.length; x++)
if (array[x] > max)
max = array[x];
return max;
}
}