简体   繁体   中英

Calculate average of user input using for each

Hi Im new to java and trying to do some exercises to familiarize myself with it. I am trying to calculate the sum and average of the user input numbers using a for each or enhanced for loop. What i have done is to store the numbers in an array. Can you please check if my code is correct.

Scanner scanner = new Scanner(System.in);

double inputNum;
double sum= 0;
int counter;

System.out.println("How many entries would you like input");
int entry = scanner.nextInt();

for (counter = 0; counter< entry; counter++){

    double [] numbers = new double[entry]; 
    System.out.println("Please input number " + (counter +1));
    numbers[counter] = scanner.nextDouble();

    for (double x : numbers[counter]) {
        x += sum;
    }

    System.out.println("The total sum is: " + sum);
    double average = sum/entry;
    System.out.println("The total average is: " + average);
}

You don't need the inner loop. You can add each input to the sum after you store it in the array.

In addition, you should allocate the array once outside the outer loop, and not in each iteration.

double [] numbers = new double[entry];
for (counter = 0; counter< entry; counter++){       
    System.out.println("Please input number " + (counter +1));
    numbers[counter] = scanner.nextDouble();
    sum+= numbers[counter];
}
System.out.println("The total sum is: " + sum);
double average = sum/entry;
System.out.println("The total average is: " + average);

Put the sum and average logic outside the loop. This should work:

double [] numbers = new double[entry]; 
for (counter = 0; counter< entry; counter++){
  System.out.println("Please input number " + (counter +1));
  numbers[counter] = scanner.nextDouble();       
  sum += numbers[counter];
}

System.out.println("The total sum is: " + sum);
double average = sum/entry;
System.out.println("The total average is: " + average);

You can't use a for-each with a scanner. Also there is no need your you to be storing anything. Just keep track of the sum.

Scanner scanner = new Scanner(System.in);

System.out.println("How many entries would you like input?");
int numEntries = scanner.nextInt();

double sum = 0;
for (counter = 1; counter <= numEntries; counter++) {
    System.out.println("Please input number " + counter + ": ");
    sum += scanner.nextDouble();
}
System.out.println("The total sum is: " + sum);
System.out.println("The average is: " + sum / numEntries);

Same code, with average outside the loop and the useless array removed:

Scanner scanner = new Scanner(System.in);

double sum = 0;

System.out.println("How many entries would you like input");
int entry = scanner.nextInt();

for (int counter = 1; counter <= entry; counter++) {
    System.out.println("Please input number " + counter);
    sum += scanner.nextDouble();
}

System.out.println("The total sum is: " + sum);
double average = sum / entry;
System.out.println("The total average is: " + average);

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