Solution: Loop Through and Analyze an Array

  • public class Main: Defines the main class that contains the program.

  • public static void main(String[] args): The entry point of the program where execution begins.

  • int[] numbers = {42, 17, 68, 33, 91};: Creates an integer array named numbers with 5 values: 42, 17, 68, 33, and 91.

  • System.out.println("Numbers in the array:");: Prints a message before displaying all numbers in the array.

  • int max = numbers[0];: Initializes a variable max with the first value in the array.This will be used to keep track of the highest number found.

  • for (int num : numbers): A for-each loop that goes through each element (num) in the array.

  • System.out.println(num);: Prints each number from the array.

  • if (num > max) { max = num; }: Compares the current number (num) with the current highest (max).If num is greater, it updates max with this new higher value.

  • System.out.println("Highest number: " + max);: After the loop, prints the highest number found in the array.

Solution: Loop Through and Analyze an Array

  • public class Main: Defines the main class that contains the program.

  • public static void main(String[] args): The entry point of the program where execution begins.

  • int[] numbers = {42, 17, 68, 33, 91};: Creates an integer array named numbers with 5 values: 42, 17, 68, 33, and 91.

  • System.out.println("Numbers in the array:");: Prints a message before displaying all numbers in the array.

  • int max = numbers[0];: Initializes a variable max with the first value in the array.This will be used to keep track of the highest number found.

  • for (int num : numbers): A for-each loop that goes through each element (num) in the array.

  • System.out.println(num);: Prints each number from the array.

  • if (num > max) { max = num; }: Compares the current number (num) with the current highest (max).If num is greater, it updates max with this new higher value.

  • System.out.println("Highest number: " + max);: After the loop, prints the highest number found in the array.