공부/코드모음
배열 최대값/ 최소값 구하기
예림220
2023. 6. 2. 14:06
[최소]
public class Test2 {
public static void main(String[] args) {
int[] array = { 1, 5, 3, 8, 2 };
int min = array[0];
for (int i = 0; i < array.length; i++) {
if (array[i] < min) {
min = array[i];
}
}
System.out.println(min);
}
}
[최대]
public class Test2 {
public static void main(String[] args) {
int[] array = { 1, 5, 3, 8, 2 };
int max = array[0];
for (int i = 0; i < array.length; i++) {
if (array[i] > max) {
max = array[i];
}
}
System.out.println(max);
}
}