How to find the min / max value using for-loop. I searched this but did not find it.I want to some help.
Asked
Active
Viewed 243 times
-12
-
In what kind of structure is your min/max value stored? A `List`? A `Set`? A `Map`? – Shankha057 Nov 07 '19 at 12:33
-
https://www.tutorialspoint.com/How-to-find-Min-Max-numbers-in-a-java-array – Shankha057 Nov 07 '19 at 12:35
-
How does your code look so far? – Laertes Nov 07 '19 at 12:36
-
Possible duplicate of [Finding the max/min value in an array of primitives using Java](https://stackoverflow.com/questions/1484347/finding-the-max-min-value-in-an-array-of-primitives-using-java) – Dinesh Shekhawat Nov 07 '19 at 12:40
-
[How to ask](https://stackoverflow.com/help/how-to-ask) – Joakim Danielson Nov 07 '19 at 12:56
1 Answers
-2
I think so is easy
int [] tabMaxMin = {1, 4, 8, -4, 11, 0};
int max = tabMaxMin[0];
int min = tabMaxMin[0];
for (int i=1; i < tabMaxMin.length; i++) {
if(min >= tabMaxMin[i])
min = tabMaxMin[i];
if(max <= tabMaxMin[i])
max = tabMaxMin[i];
}
System.out.println("MAX=" + max);
System.out.println("MIN=" + min);
}

Pratik
- 1,351
- 1
- 20
- 37

WojtekGalach
- 16
- 4