简体   繁体   中英

How to calculate an average of a range?

I need assistance for a homework question. The question is as follows: "Compute the sum and average from a range of numbers using a while loop and a do while loop. Display the results of each loop." I already have most of the code finished, I'm just having issues with the average part of it. Here is what I have so far.

import java.util.Scanner;

public class Homework {
    public static Scanner input = new Scanner(System.in);
    public static void main(String[] args) {
        //Get the minimum and maximum values for the range.
        System.out.print("Enter the minimum for the range: ");
        int number1 = input.nextInt();
        System.out.print("Enter the maximum for the range: ");
        int number2 = input.nextInt();
        int sum = 0;
        double average = 0;
        while (sum == 0){
            //Get the sum
            for (int i = number1; i <= number2; i++)
                sum = sum + i;
            for (int i = number1; i <= number2; i++)
                average = sum / i;
            System.out.println("(while loop)The sum is " + sum + " and the average is " + average + ".");
        }
        do {
            //reset the sum so it doesn't mess up.
            sum = 0;
            for (int i = number1; i <= number2; i++)
                sum = sum + i;
            for (int i = number1; i <= number2; i++)
                average = sum / i;
            System.out.println("(do-while loop)The sum is " + sum + " and the average is " + average + ".");
        } while (sum == 0);
    }
}

Now, my main issue is trying to get the average for a range where the minimum isn't 1. It works perfectly fine as long as the minimum value in the range is 1. Is there any way I could get it to work with a minimum value of more than 1.

int sum = 0;
int counter = number1;
while(counter <= number2){
   sum += counter;
   counter++;
}
System.out.println("Sum:\t" + sum);
System.out.println("Average:\t" + (sum / (number2 - number1 + 1));
//--------------------------------------------------
sum = 0;
counter = number1;
do{
   sum += counter;
   counter++;
}while(counter <= number2);
System.out.println("Sum:\t" + sum);
System.out.println("Average:\t" + (sum / (number2 - number1 + 1));

The condition of your while loop (sum == 0) made no sense. If you want to iterate over a range, the end of that range should be your condition. Thinking about the relationship between a sum and an average makes it clear you only need to loop once and then divide the result. You could repeat the loops (as you tried to do) if you wish but it's slower and unnecessary.

Cheers!

Edit: I just wanted to add this clarification which I hope will explain things a little further: The way you had written your code, your while / do-while loops would have only run once (just like if you did for (int i = 0; i < 1; i++) ) and you were actually doing the looping using the more familiar for loop inside the while loop. However, as another commenter pointed out the purpose of this assignment was probably to get you to see how you can use a while loop instead of using the more intuitive for loop -- and by falling back on to the for loop instead you proved your teacher right a little :)

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