如何在Java中从命令行平均数字

时间:2014-01-25 21:43:46

标签: java double average

这是一项学校作业。我真的很想了解我在做什么。我们被指示使用命令行运行Java程序。用户输入数字后,程序应该开始,发送欢迎信息,然后告诉用户他的号码的平均值是多少。

我认为这一点是为了说明如何在程序中使用args并帮助我们熟悉Java。

这是我目前的解释。我可以从命令行运行它,我知道如何添加欢迎消息...但我不能很好地理解代码,以便能够在用户添加参数时添加平均数字的能力。

public class ArgumentExample {

    public static void main(String[] args) {

        if( args.length == 1 || args.length > 1 ){
            System.out.println( args[0]);
        }

        if( args.length > 1 ) {
            for( int i = 1; i < args.length; i++ ){
                System.out.println(args[i]);
            }
        }
    }
}

5 个答案:

答案 0 :(得分:2)

public class ArgumentExample {

        public static void main(String[] args) {
            int i = 0; // declare a counter here so you can use it in the catch
            double tot = 0.0d; // declare the accumulator variable
            try {
                for (i = 0; i < args.length; i++) { // iterate over arguments...if only one is present returns just that one
                    tot += Double.parseDouble(args[i]); // sum
                }
            } catch (Exception ex) {
                System.out.println("Argument " + i + " is not a number");// print which argument is not a number if any
            }
            System.out.println("Sum is: " + tot/(args.lenght == 0 ? 1 : args.lenght); // final print statement
        }
    }

答案 1 :(得分:1)

现在您只需打印出放入args数组的参数。将String参数转换为数字(比如说,双精度数),将它们添加到循环中的运算总和,然后除以循环后的参数数量,这不是一个很大的步骤:

    double sum = 0;
    if (args.length >= 1) {
        for (int i = 0; i < args.length; i++) {
            sum += Double.parseDouble(args[i]);
        }
        System.out.println("Average = " + sum/args.length);
    }

Double是基本类型double的包装类。它有一个方便的parseDouble方法,你可以传递String,它会尝试将其转换为double类型。我们应该在这里使用双打而不是整数,因为双精度的精度更高 - 例如如果除以5/2,则得到2,而不是2.5,除非你确定5和2被视为双打。

请注意,这没有错误检查,因此如果任何参数不能解析为Double,则抛出异常会失败。

答案 2 :(得分:1)

args是对String数组的引用,该数组包含启动时传递给程序的所有参数。

数组使用0基系统来引用该位置的对象,因此args[0]是传递给程序的第一个参数。

for( int i = 1; i < args.length; i++ )循环 正在遍历数组,直到打印完每个参数后到达结束( i = length - 1 )

答案 3 :(得分:1)

学生往往会把太多的东西放进主场。

我建议你把真正的工作放在合理的方法中,有朝一日你可以使用它并传入和传出数据,如下所示:

public class StatisticsUtils {
    public double average(double [] values) {
        double average = 0.0;
        if ((values != null) && (values.length > 0)) {
            for (double value : values) {
                average += value;
            }
            average /= values.length; 
        }
        return average;
    }
}

答案 4 :(得分:1)

要获得数字的平均值,您必须构建它们的总和,然后除以参数的数量。例如

public class ArgumentExample 
{
  public static void main(String[] args) {

  int sum = 0;

  // step through all arguments
  for(String arg : args)
  {
    try
    {
      // try to convert the argument to an integer
      int number = Integer.parseInt(arg);
      // sum it
      sum += integer;
    }
    catch(NumberFormatException e)
    {
      // the currently processed argument couldn't be converted
      // to a number
      System.out.println(arg + " is not a number");
    }
  }

  // average = sum / valuecount
  double average = sum / args.length;

  System.out.println("average = " + average);
}

java -jar ArgumentExample 1 2 3 4 5将导致3.希望有所帮助

相关问题