简体   繁体   English

在用户控制的循环程序中计算平均值

[英]Calculate number average in user controlled looping program

I'm having trouble on another Java program, and it's probably something really small, but for some reason, I can't understand what the issue is. 我在另一个Java程序上遇到了麻烦,它可能确实很小,但是由于某种原因,我无法理解问题所在。 Similar questions have been brought up many times before, but it doesn't give the exact same thing needed for this program, at least the answers being given didn't help. 类似的问题以前曾被提出过很多次,但是它并没有给出该程序所需的确切信息,至少给出的答案没有帮助。

The program needs to calculate the average of the amount set of numbers that the user initially inputs. 该程序需要计算用户最初输入的数字数量集的平均值。 Which is declared as inputNumber . 声明为inputNumber The program then asks the user for each number of the set amount the user specified. 然后,程序向用户询问用户指定的设置数量的每个数字。 This is addedNumber , and all of them are added to a total, which is numberSum , and is then divided by inputNumber to get the total's average, which value is declared as numberAverage . 这是addedNumber ,并将它们全部加到一个总数中,即numberSum ,然后除以inputNumber以获得总数的平均值,该平均值被声明为numberAverage

For example: If the program prompts me for how many numbers I want to input and I type 4 ( inputNumber ), the scanner prompts me for a number 4 times ( addedNumber ), and I type in 4 numbers, such as the numbers: 2, 3, 6, and 1. Those numbers are added together as the sum 12 ( numberSum ), and then divided to get an average of the 4 numbers ( numberAverage ). 例如:如果程序提示我要输入多少个数字,并且我键入4( inputNumber ),则扫描程序会提示我输入4个数字( addedNumber ),然后我键入4个数字,例如数字:2 、、 3、6和1。这些数字加在一起作为和12( numberSum ),然后除以得到4个数字的平均值( numberAverage )。

The counter i that will calculate how many times to prompt the user will increase until it reaches the number that the user inputted to have calculated is declared separately in the for loop. 用于计算提示用户次数的计数器i将在for循环中单独声明,直到达到用户输入的计算数量为止。 And the whole program happens on a while loop until the user inputs 0 as the value of inputNumber , which will then break the loop and terminate the program. 整个程序会在while循环中进行,直到用户输入0作为inputNumber的值inputNumber ,这将中断循环并终止程序。

Here's the code: 这是代码:

import java.util.Scanner;
/**
 * Write a description of class AverageNumbers here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Average2
{
    public static void main ()
    {
        int inputNumber;
        double numberSum = 0;

        Scanner scan = new Scanner(System.in); 

        while (true) {
            System.out.print("Please enter the amount of numbers you want to be calculated. (0 to quit): ");
            inputNumber = scan.nextInt();

            if (inputNumber == 0) {
                System.out.println("Program Ended");
                break; 
            }
            for ( int i = 0; i < inputNumber; i++) {
                System.out.print("Please enter a value:" );

                double addedNumber = scan.nextInt();
                numberSum += addedNumber;
            }

            double numberAverage = numberSum/inputNumber;

            System.out.println("Average: " + numberAverage);
        }
    }
}

But here's the problem: 但这是问题所在:

Loop Error 循环错误

As you can see, only the first part of the loop gets the average right, but the loop going the second time around doesn't. 如您所见,只有循环的第一部分获得了正确的平均值,而第二次循环却没有。 I'm stuck and how the loop is supposed to go so that the average is calculated right every time instead of just the first. 我很困惑,应该如何进行循环,以便每次都可以正确计算平均值,而不仅仅是第一次。 I think it has to do with the initialization, but initializing the variable to 0 , 1 , or -1 didn't help. 我认为这与初始化的事,但初始化变量为01-1并没有帮助。 Plus setting the variable to 0 renders the program to never run since that's the condition in order to stop it. 加上将变量设置为0会使程序永远无法运行,因为这是停止程序的条件。 If it does so happen to be a simple initialization error, could someone please just explain what is going on that's wrong with the code instead of saying that this question has been asked before, please? 如果确实是一个简单的初始化错误,请问有人可以解释一下代码的问题是什么,而不是先问这个问题?

The code as written is giving the average of every number you've ever entered rather than just the average of the group just entered. 编写的代码给出的是您输入的每个数字的平均值,而不是所输入组的平均值。

You need to "reset" numberSum ( numberSum = 0; ) at the start of each group of numbers. 您需要在每组数字的开头“重置” numberSumnumberSum = 0; )。

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

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