Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Challenge: Find Maximum and Minimum in Array | Methods
Java Extended

book
Challenge: Find Maximum and Minimum in Array

Taak

Swipe to start coding

Your task is to write two methods: one to find the maximum value and one to find the minimum value in the array.

  1. In the findMax method, initialize the max variable with the first element of the array.
  2. In the for loop, compare the current element with max.
  3. If the current element is greater, update max with the current element.
  4. Return the value of the max variable.
  5. In the findMin method, initialize the min variable with the first element of the array.
  6. In the for loop, compare the current element with min.
  7. If the current element is smaller, update min with the current element.
  8. Return the value of the min variable.
  9. In the main method, call the findMax method with the correct parameter, pass the numbers array into it, and assign the result to the max variable.
  10. In the main method, call the findMin method with the correct parameter, pass the numbers array into it, and assign the result to the min variable.

Oplossing

java

solution

package com.example;

public class Main {
static int findMax(int[] array) {
int max = array[0];
for (int number : array) {
if (number > max) {
max = number;
}
}
return max;
}

static int findMin(int[] array) {
int min = array[0];
for (int number : array) {
if (number < min) {
min = number;
}
}
return min;
}

public static void main(String[] args) {
int[] numbers = {10, -3, 25, 7, 99, -50, 0, 12};

int max = findMax(numbers);
int min = findMin(numbers);

System.out.println("Maximum: " + max);
System.out.println("Minimum: " + min);
}
}
Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 8
package com.example;

public class Main {
static int findMax(int[] array) {
int max = array[___];
for (int number : array) {
if (___ > max) {
max = ___;
}
}
return ___;
}

static int findMin(int[] array) {
int min = array[___];
for (int number : array) {
if (___ < min) {
min = ___;
}
}
return ___;
}

public static void main(String[] args) {
int[] numbers = {10, -3, 25, 7, 99, -50, 0, 12};

int max = ___;
int min = ___;

System.out.println("Maximum: " + max);
System.out.println("Minimum: " + min);
}
}

Vraag AI

expand
ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

some-alt