Calculate average in java

后端 未结 10 2134
悲哀的现实
悲哀的现实 2020-12-11 07:49

EDIT: Ive written code for the average, but i dont know how to make it so that it also uses ints from my args.length rather than the array

I need to

相关标签:
10条回答
  • 2020-12-11 08:01

    Just some minor modification to your code will do (with some var renaming for clarity) :

    double sum = 0; //average will have decimal point
    
    for(int i=0; i < args.length; i++){
       //parse string to double, note that this might fail if you encounter a non-numeric string
       //Note that we could also do Integer.valueOf( args[i] ) but this is more flexible
       sum += Double.valueOf( args[i] ); 
    }
    
    double average = sum/args.length;
    
    System.out.println(average );
    

    Note that the loop can also be simplified:

    for(String arg : args){
       sum += Double.valueOf( arg );
    }
    

    Edit: the OP seems to want to use the args array. This seems to be a String array, thus updated the answer accordingly.

    Update:

    As zoxqoj correctly pointed out, integer/double overflow is not taken care of in the code above. Although I assume the input values will be small enough to not have that problem, here's a snippet to use for really large input values:

    BigDecimal sum = BigDecimal.ZERO;
    for(String arg : args){
      sum = sum.add( new BigDecimal( arg ) );
    }
    

    This approach has several advantages (despite being somewhat slower, so don't use it for time critical operations):

    • Precision is kept, with double you will gradually loose precision with the number of math operations (or not get exact precision at all, depending on the numbers)
    • The probability of overflow is practically eliminated. Note however, that a BigDecimal might be bigger than what fits into a double or long.
    0 讨论(0)
  • 2020-12-11 08:02

    for 1. the number of integers read in, you can just use length property of array like :

    int count = args.length
    

    which gives you no of elements in an array. And 2. to calculate average value : you are doing in correct way.

    0 讨论(0)
  • 2020-12-11 08:03
     System.out.println(result/count) 
    

    you can't do this because result/count is not a String type, and System.out.println() only takes a String parameter. perhaps try:

    double avg = (double)result / (double)args.length
    
    0 讨论(0)
  • 2020-12-11 08:07

    I'm going to show you 2 ways. If you don't need a lot of stats in your project simply implement following.

    public double average(ArrayList<Double> x) {
        double sum = 0;
        for (double aX : x) sum += aX;
        return (sum / x.size());
    }
    

    If you plan on doing a lot of stats might as well not reinvent the wheel. So why not check out http://commons.apache.org/proper/commons-math/userguide/stat.html

    You'll fall into true luv!

    0 讨论(0)
  • 2020-12-11 08:07
    public class MainTwo{
        public static void main(String[] arguments) {
            double[] Average = new double[5];
            Average[0] = 4;
            Average[1] = 5;
            Average[2] = 2;
            Average[3] = 4;
            Average[4] = 5;
            double sum = 0;
            if (Average.length > 0) {
                for (int x = 0; x < Average.length; x++) {
                    sum+=Average[x];
                    System.out.println(Average[x]);
                }
                System.out.println("Sum is " + sum);
                System.out.println("Average is " + sum/Average.length);
          }
       }
    }
    
    0 讨论(0)
  • 2020-12-11 08:08
    int values[] = { 23, 1, 5, 78, 22, 4};
    
    int sum = 0;
    for (int i = 0; i < values.length; i++)
        sum += values[i];
    
    double average = ((double) sum) / values.length;
    
    0 讨论(0)
提交回复
热议问题