在命令行中携带若干个数值,找出这些数中的最大值、最小值和平均值

1个回答

  • 我估计你问的应该是java的吧,下面是代码和截图,希望对你有用.

    import java.util.Arrays;

    public class NumberHandle {

    public static void main(String []args){

    //String arr[] = {"1","2","5","4","3"};

    System.out.println("最大值:"+getMax(args));

    System.out.println("最小值:"+getMin(args));

    System.out.println("平均值:"+getAverage(args));

    }

    //最大值

    public static int getMax(String arr[]){

    int ar[] = new int[arr.length];

    for(int s = 0;s < arr.length;s ++){

    ar[s] = Integer.parseInt(arr[s]);

    }

    int l = ar.length;

    Arrays.sort(ar);

    int max = ar[l -1];

    return max;

    }

    //最小值

    public static int getMin(String arr[]){

    int ar[] = new int[arr.length];

    for(int s = 0;s < arr.length;s ++){

    ar[s] = Integer.parseInt(arr[s]);

    }

    int l = ar.length;

    Arrays.sort(ar);

    int min = ar[0];

    return min;

    }

    //平均值

    public static int getAverage(String[] arr){

    int ar[] = new int[arr.length];

    for(int s = 0;s < arr.length;s ++){

    ar[s] = Integer.parseInt(arr[s]);

    }

    int l = ar.length;

    int sum = 0;

    for(int i = 0;i < l;i ++){

    sum = sum+ar[i];

    }

    int average = sum / l;

    return average;

    }

    }