返回由数字组成的字符串的最大值和最小值(Highest and Lowest)

public class Else {
    public static void main(String[] args) {
        String onestr = "1 2 -3 4 5";
        String[] first = onestr.split(" ");
        Integer[] arr = new Integer[first.length];
        for (int i = 0; i < first.length; i++) {
            arr[i] = Integer.parseInt(first[i]);
        }
        Integer high = arr[0];
        Integer towel = arr[0];
        for (int i = 0; i < arr.length; i++) {
            if (high < arr[i]) {
                high = arr[i];
            }
            if (towel > arr[i]) {
                towel = arr[i];
            }
        }
        System.out.println(high.toString() + " " + towel.toString());
    }
}

将由数字组成的字符串转化为数组:
arr[i] = Integer.parseInt(first[i]);
arr[i] = Integer.valueOf(first[i]);

在定义数字或者数组的时候用Integer可以在后续方便使用各种方法

posted @ 2020-05-18 11:14  半羽  阅读(274)  评论(0)    收藏  举报