为了账号安全,请及时绑定邮箱和手机立即绑定

Java程序计算变量数组的平均值,从命令行输入,显示结果

Java程序计算变量数组的平均值,从命令行输入,显示结果

DIEA 2021-04-07 13:11:18
以下是我到目前为止的内容。这些说明是为了创建一个简单的Java程序,该程序计算可变数量的int的平均值。主要的Args数组,无Scanner对象,查找并显示最高和最低值。将用户输入作为参数并返回值的静态方法。启动时应显示欢迎消息。因此,我认为代码的开头是正确的!它为我编译。但是我不确定如何在不使用扫描仪的情况下获得用户输入。我假设我需要在静态方法中使用数组将输入转换为参数。执行时哪个是Java CalculateAverage 1、2、5、9、20?然后我会打电话给MATH吗?这样我就可以显示min max和avg的所有值?我在正确的轨道上吗?这些问题在代码中是特定的。抱歉,第一次在这里发布!public class CalculateAverage {public static void main(String[] args) {    System.out.print("Hello welcome to the Average Calculator");    int[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};    int a = Integer.parseInt(args[0]);    int b = Integer.parseInt(args[1]);    //do i just keep going and adding letters til j?     //or is there an easier way to do this..?    minArray(array);    maxArray(array);    sumArray(array);    avgArray(array);}public static double maxArray(double[] array){    double max = Double.NEGATIVE_INFINITY;       //tbh i saw this on a tutorial, idk if NEGATIVE_INFINITY would        //work for me?         for (int i = 0; i < n; i++)            if (array[i] > max) max = array[i];    System.out.println("The maximum of your array is:" + max);}public static double avgArray(double[] array){    double sum = 0.0;    for(int i = 0; i < n; i++)        sum += array[i];    double avg = sum / n;    System.out.println("The average value of your array is: " + avg);} //then i would make another static for the min but probably before max}
查看完整描述

3 回答

?
跃然一笑

TA贡献1826条经验 获得超6个赞

这就是我会做的。希望代码是简单且不言自明的,但是如果有任何问题,请告诉我:


public static void main(String[] args) {

    System.out.println("Hello welcome to the Average Calculator");

    int numArgs = args.length;  //Since args is an array we can get the number of elements with .length

    int min = Integer.MAX_VALUE;  //The maximum possible value an int can be

    int max = Integer.MIN_VALUE;  //The minimum possible value an int can be

    double total = 0;

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

        int nextI = Integer.parseInt(args[i]);

        total += nextI;

        if(nextI < min) {

            min = nextI;

        }

        if(nextI > max) {

            max = nextI;

        }

    }

    System.out.println("The average is: " + total/numArgs);

    System.out.println("The min is: " + min);

    System.out.println("The max is: " + max);

}

然后,您将运行如下代码:


java CalculateAverages 1 2 3 4 5 6 7 8 9


Hello welcome to the Average Calculator

The average is: 5.0

The min is: 1

The max is: 9

编辑:


指令说:“要找到这些值,请创建将用户输入作为参数并返回值的静态方法”


public static void main(String[] args) {

    int numArgs = args.length;

    int[] userIntInputs = new int[numArgs]; 

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

        userIntInputs[i] = Integer.parseInt(args[i]);

    }

    System.out.println("The average is: " + getInputAverage(userIntInputs));

    System.out.println("The min is: " + getInputMin(userIntInputs));

    System.out.println("The max is: " + getInputMax(userIntInputs));

}


private static int getInputMax(int[] userIntInputs) {

    int max = Integer.MIN_VALUE;

    for(int i = 0; i < userIntInputs.length; i++) {

        if(userIntInputs[i] > max) {

            max = userIntInputs[i];

        }

    }

    return max;

}


private static int getInputMin(int[] userIntInputs) {

    int min = Integer.MAX_VALUE;

    for(int i = 0; i < userIntInputs.length; i++) {

        if(userIntInputs[i] < min) {

            min = userIntInputs[i];

        }

    }

    return min;

}


private static double getInputAverage(int[] userIntInputs) {

    double total = 0;

    for(int i = 0; i < userIntInputs.length; i++) {

        total += userIntInputs[i];

    }

    return total/userIntInputs.length;

}

一种方法替代


如果她想为每个值或一个方法创建一个方法,则为Idk!


是的,老师可能会混淆。这是一种方法...


public static void main(String[] args) {

    int numArgs = args.length;

    int[] userIntInputs = new int[numArgs]; 

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

        userIntInputs[i] = Integer.parseInt(args[i]);

    }


    Object[] inputMetrics = getInputMetrics(userIntInputs);

    System.out.println("The average is: " + inputMetrics[0]);

    System.out.println("The min is: " + inputMetrics[1]);

    System.out.println("The max is: " + inputMetrics[2]);


}


private static Object[] getInputMetrics(int[] userIntInputs) {

    int min = Integer.MAX_VALUE; 

    int max = Integer.MIN_VALUE;

    double total = 0;

    for(int i = 0; i < userIntInputs.length; i++) {

        int nextI = userIntInputs[i];

        total += nextI;

        if(nextI < min) {

            min = nextI;

        }

        if(nextI > max) {

            max = nextI;

        }

    }

    Object[] metrics = {total/userIntInputs.length, min, max};

    return metrics;

}


查看完整回答
反对 回复 2021-04-21
?
杨魅力

TA贡献1811条经验 获得超6个赞

简单是恕我直言的最佳方法。这完成了整个事情:


DoubleSummaryStatistics stats = Arrays.stream(args)

    .collect(Collectors.summarizingDouble(Double::parseDouble));


System.out.println("The minimum of your array is: "+ stats.getMin());

System.out.println("The maximum of your array is: "+ stats.getMax());

System.out.println("The sum of your array is: "+ stats.getSum());

System.out.println("The average of your array is: "+ stats.getAverage());

当有内置库可以处理某些事情时,请使用它。


查看完整回答
反对 回复 2021-04-21
  • 3 回答
  • 0 关注
  • 156 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信