简体   繁体   English

如何计算范围的平均值?

[英]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." 问题如下:“使用while循环和do while循环从一定范围的数字计算总和和平均值。显示每个循环的结果。” 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. 现在,我的主要问题是尝试获取最小值不为1的范围内的平均值。只要范围内的最小值为1,它就可以很好地工作。有什么办法可以使它与a最小值大于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. while循环的条件 (sum == 0)没有任何意义。 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. 编辑:我只是想添加一下说明,希望可以进一步解释一下:您编写代码的方式, while / do-while循环只会运行一次(就像您for (int i = 0; i < 1; i++)和你实际上正在做使用用于 while循环循环的更熟悉的循环。 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 :) 但是,正如另一位评论者所指出的,此作业的目的可能是让您了解如何使用while循环而不是使用更直观的for循环-并通过回到for循环来证明您的老师正确一点:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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