Answer the question
In order to leave comments, you need to log in
How to find min and max?
Hello!
In general, I encountered such a problem, we were asked to make an algorithm for finding the location of mines and max in an array.
Here's what I've done so far (but even it doesn't work properly)
public static void main(String[] args) {
ArrayList<Integer> wyrazy= new ArrayList<>();
Random generator = new Random();
int i = 0;
int n_n=100;
int n = 10;
int b = 0;
while(i!=n)
{
int random=generator.nextInt(n_n);
wyrazy.add(random);
i++;
}
System.out.println(wyrazy);
int t = 0;
int min = wyrazy.get(t);
System.out.println(min);
while(t<wyrazy.size()-1)
{
t++;
while(wyrazy.get(t)<min)
{min = wyrazy.get(t);
b++;
}
}
System.out.println(b);
System.out.println(min);
System.out.println(wyrazy.get(t));
}
Answer the question
In order to leave comments, you need to log in
import java.util.Arrays;
public class Test {
public static void main(String[] args){
int[] tab = {12, 1, 21, 8};
int min = Arrays.stream(tab).min().getAsInt();
int max = Arrays.stream(tab).max().getAsInt();
System.out.println("Min = " + min);
System.out.println("Max = " + max)
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question