java find the lowest biggest number in an array code example

Example 1: find the largest and smallest number in an unsorted integer array in Java

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Number of integers to enter:");
    int numberEnter = keyboard.nextInt();
    int numbers[] = new int[numberEnter];
    int pos = 0;
    do {
        System.out.printf("Please enter integer #%d/%d:%n", pos, numberEnter);
        numbers[pos++] = keyboard.nextInt();
    } while (pos < numberEnter && keyboard.hasNextInt());
    int min = numbers[0];
    int max = numbers[0];
    for (pos = 1; pos < numbers.length; pos++) {
        if (numbers[pos] < min) { // <-- test min.
            min = numbers[pos];
        }
        if (numbers[pos] > max) { // <-- test max.
            max = numbers[pos];
        }
    }
    // Display everything.
    System.out.printf("%s Min: %d Max: %d%n", Arrays.toString(numbers),
            min, max);
}

Example 2: java find biggest number in array

for (int counter = 1; counter < decMax.length; counter++)
{
     if (decMax[counter] > max)
     {
      max = decMax[counter];
     }
}

System.out.println("The highest maximum for the December is: " + max);

Tags:

Java Example