简体   繁体   中英

Moving Average Using User-Input Array

I need to write a program that calculates a moving average by a user inputted array. The first element of the array is the window size, and the input is terminated by a 0. The output values are printed with two digits after the decimal point.

Example input: 3 2 4 7 7 8 11 12 0

Corresponding Output: 4.33 6.00 7.33 8.67 10.33

(4.33 is average of 2,4,7 and 6 is average of 4,7,7 etc.)

Here's my code so far:

package movingaverage;

import java.util.Scanner;

public class MovingAverage {

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int[] arr = new int[n];
    int sum = 0;
    for (int i = 0; i < n; i++) {
        sum += arr[i];
     }
     avg[0] = sum / 5;

    int j = 1;
    for (int i = 5; i < arr.length; i++) {
        sum = sum + arr[i] - arr[i - 5];
        avg[j++] = sum / 5;
    }

  }

}

I think I have the loop right, but I'm not sure how to get the array to end at 0.

This is a possible solution.

public class Test
{
   private static final Scanner SCANNER;

   static {
      SCANNER = new Scanner(System.in);
   }

   public static final void main(final String... args) {
      final String[] numbers = SCANNER.nextLine().trim().split(" ");
      final int consideredElements = Integer.parseInt(numbers[0]);
      float sum = 0;
      int value = 0;

      for (int i = 1; i < numbers.length; i++) {
         sum = 0;

         for (int k = 0; k < consideredElements; k++) {
            value = Integer.parseInt(numbers[i + k]);

            if (value == 0) {
               return;
            }

            sum += value;
         }

         System.out.println(new BigDecimal(sum / consideredElements).setScale(2, RoundingMode.HALF_EVEN));
      }
   }
}

First, you are using 5 in a couple of places in your program, I see no justification for that. Could it be that your expectation of user input lead you to put 5 where the number you really should use, depends on user input? Maybe you should use the window size instead? I'm guessing a bit here.

Next, as @lppEdd pointed out, you are not reading the numbers from your input — only the window size.

Next, you are declaring your array of size n , which I believe was your window size, not your array size. I believe the real solution to this problem is using better and more explanatory variable names.

Your code does not compile since you have not declared the array avg that you try to store your moving average into.

Fifth, when you want your average as a double , you need to convert to double before dividing (this is a classic pitfall that has already generated many questions on Stack Overflow).

I hope this gets you a couple of steps further.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM