查找数字的平均值(使用 while 循环;java)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33278601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:28:33  来源:igfitidea点击:

Find average of numbers (with while loops; java)

javawhile-loopuser-input

提问by Aron_dc

My code stops when the user inputs -1 (like it should) but the average printed is wrong.

我的代码在用户输入 -1 时停止(就像它应该的那样)但打印的平均值是错误的。

This is what I have:

这就是我所拥有的:

import static java.lang.System.*;
import java.util.Scanner;
import java.util.*;

class averages_1
{
    public static void main(String[] args)   
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter the scores:");
        double score = 0;
        double num = 0;
        double sum = 0;

    while (score != -1)
    {
        score = scan.nextDouble();
        sum += score;
        num++;
    }
    System.out.println("The average is:" + (sum/num));
}

}

If you enter 50 then 105 then -1, the output is

如果您输入 50 然后是 105 然后是 -1,则输出为

Enter the scores:

50

105

-1

The average is: 51.333333333333336

I just need to correct the average. Any help is appreciated!

我只需要修正平均值。任何帮助表示赞赏!

回答by Eran

You include the -1 in your average.

您将 -1 包括在您的平均值中。

One way to avoid it is to add another condition :

避免它的一种方法是添加另一个条件:

while (score != -1)
{
    score = scan.nextDouble();
    if (score != -1) {
        sum += score;
        num++;
    }
}

However, a more elegant solution would read the first input before the loop, and only add valid inputs :

但是,更优雅的解决方案是在循环之前读取第一个输入,并且只添加有效输入:

score = scan.nextDouble();
while (score != -1)
{
    sum += score;
    num++;
    score = scan.nextDouble();
}

In general, changing the variable used in the condition of the while loop should be the last thing you do in the iteration.

通常,更改 while 循环条件中使用的变量应该是您在迭代中做的最后一件事。

回答by nafas

you can read input within the condition of your while loopinstead, this way as soon as you read -1you quit the loop and it won't be added to your sumvariable

你可以在你的条件内读取输入while loop,这样一旦你读到-1你就退出循环并且它不会被添加到你的sum变量中

while ((score=scan.nextDouble()) != -1)
{
    sum += score;
    num++;
}

回答by Aron_dc

You just add the last -1 to your sum and still count it as number. You have to break;your loop if your input was -1.

您只需将最后一个 -1 添加到您的总和中,并仍将其视为数字。break;如果您的输入为 -1,则您必须进行循环。

回答by ΦXoc? ? Пepeúpa ツ

You are increasing numeven for input==-1, which is not a valid input, but a escape character...

num甚至在增加for input==-1,这不是一个有效的输入,而是一个转义字符......

Either modify the logic or just correct the snippet:

修改逻辑或仅更正代码段:

System.out.println("The average is:" + (++sum/--num));

回答by Iqbal Shirol

Change your code as below

更改您的代码如下

import static java.lang.System.*;
import java.util.Scanner;
import java.util.*;

class averages_1 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter the scores:");
        double score = 0;
        double num = 0;
        double sum = 0;
        while (score != -1) {
            sum += score;
            score = scan.nextDouble();
            num++;
        }
        num--;
        System.out.println("The average is:" + (sum/num));
    }
}

回答by user1702875

Ok so then just use this

好的,那么就用这个

while (score != -1) {
        if (scope != -1){
           sum += score;
           score = scan.nextDouble();
           num++;
        }
    }

It's not pretty, but like everyone else is telling you, don't add -1 to score since its your escape character.

它并不漂亮,但就像其他人告诉您的那样,不要将 -1 加到分数上,因为它是您的转义字符。