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

以下是我到目前为止的内容。这些说明是为了创建一个简单的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

}


DIEA
浏览 157回答 3
3回答

跃然一笑

这就是我会做的。希望代码是简单且不言自明的,但是如果有任何问题,请告诉我:public static void main(String[] args) {&nbsp; &nbsp; System.out.println("Hello welcome to the Average Calculator");&nbsp; &nbsp; int numArgs = args.length;&nbsp; //Since args is an array we can get the number of elements with .length&nbsp; &nbsp; int min = Integer.MAX_VALUE;&nbsp; //The maximum possible value an int can be&nbsp; &nbsp; int max = Integer.MIN_VALUE;&nbsp; //The minimum possible value an int can be&nbsp; &nbsp; double total = 0;&nbsp; &nbsp; for(int i = 0; i < numArgs; i++) {&nbsp; &nbsp; &nbsp; &nbsp; int nextI = Integer.parseInt(args[i]);&nbsp; &nbsp; &nbsp; &nbsp; total += nextI;&nbsp; &nbsp; &nbsp; &nbsp; if(nextI < min) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; min = nextI;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if(nextI > max) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; max = nextI;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println("The average is: " + total/numArgs);&nbsp; &nbsp; System.out.println("The min is: " + min);&nbsp; &nbsp; System.out.println("The max is: " + max);}然后,您将运行如下代码:java CalculateAverages 1 2 3 4 5 6 7 8 9Hello welcome to the Average CalculatorThe average is: 5.0The min is: 1The max is: 9编辑:指令说:“要找到这些值,请创建将用户输入作为参数并返回值的静态方法”public static void main(String[] args) {&nbsp; &nbsp; int numArgs = args.length;&nbsp; &nbsp; int[] userIntInputs = new int[numArgs];&nbsp;&nbsp; &nbsp; for(int i = 0; i < numArgs; i++) {&nbsp; &nbsp; &nbsp; &nbsp; userIntInputs[i] = Integer.parseInt(args[i]);&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println("The average is: " + getInputAverage(userIntInputs));&nbsp; &nbsp; System.out.println("The min is: " + getInputMin(userIntInputs));&nbsp; &nbsp; System.out.println("The max is: " + getInputMax(userIntInputs));}private static int getInputMax(int[] userIntInputs) {&nbsp; &nbsp; int max = Integer.MIN_VALUE;&nbsp; &nbsp; for(int i = 0; i < userIntInputs.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; if(userIntInputs[i] > max) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; max = userIntInputs[i];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return max;}private static int getInputMin(int[] userIntInputs) {&nbsp; &nbsp; int min = Integer.MAX_VALUE;&nbsp; &nbsp; for(int i = 0; i < userIntInputs.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; if(userIntInputs[i] < min) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; min = userIntInputs[i];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return min;}private static double getInputAverage(int[] userIntInputs) {&nbsp; &nbsp; double total = 0;&nbsp; &nbsp; for(int i = 0; i < userIntInputs.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; total += userIntInputs[i];&nbsp; &nbsp; }&nbsp; &nbsp; return total/userIntInputs.length;}一种方法替代如果她想为每个值或一个方法创建一个方法,则为Idk!是的,老师可能会混淆。这是一种方法...public static void main(String[] args) {&nbsp; &nbsp; int numArgs = args.length;&nbsp; &nbsp; int[] userIntInputs = new int[numArgs];&nbsp;&nbsp; &nbsp; for(int i = 0; i < numArgs; i++) {&nbsp; &nbsp; &nbsp; &nbsp; userIntInputs[i] = Integer.parseInt(args[i]);&nbsp; &nbsp; }&nbsp; &nbsp; Object[] inputMetrics = getInputMetrics(userIntInputs);&nbsp; &nbsp; System.out.println("The average is: " + inputMetrics[0]);&nbsp; &nbsp; System.out.println("The min is: " + inputMetrics[1]);&nbsp; &nbsp; System.out.println("The max is: " + inputMetrics[2]);}private static Object[] getInputMetrics(int[] userIntInputs) {&nbsp; &nbsp; int min = Integer.MAX_VALUE;&nbsp;&nbsp; &nbsp; int max = Integer.MIN_VALUE;&nbsp; &nbsp; double total = 0;&nbsp; &nbsp; for(int i = 0; i < userIntInputs.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; int nextI = userIntInputs[i];&nbsp; &nbsp; &nbsp; &nbsp; total += nextI;&nbsp; &nbsp; &nbsp; &nbsp; if(nextI < min) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; min = nextI;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if(nextI > max) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; max = nextI;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; Object[] metrics = {total/userIntInputs.length, min, max};&nbsp; &nbsp; return metrics;}

杨魅力

简单是恕我直言的最佳方法。这完成了整个事情:DoubleSummaryStatistics stats = Arrays.stream(args)&nbsp; &nbsp; .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());当有内置库可以处理某些事情时,请使用它。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java