1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| public class GetMinMaxString { public String getMinMaxString(String str) { String[] array = str.split(" "); int max = Integer.parseInt(array[0]); int min = Integer.parseInt(array[0]); if(array.length != 0) { for (int i = 0; i < array.length; i++) { max = Math.max(max, Integer.parseInt(array[i])); min = Math.min(min, Integer.parseInt(array[i])); } } return Integer.toString(min) + " " +Integer.toString(max); }
public static void main(String[] args) { String str = "-13 -2 -4 16"; GetMinMaxString minMax = new GetMinMaxString(); System.out.println("최대값과 최소값은?" + minMax.getMinMaxString(str)); } }
|