简体   繁体   English

JAVA:查找双数组列表的平均值

[英]JAVA: Find the average of a double array list

Trying to enter an arraylist of doubles into the console, then when the user ends the arraylist input with ctrl-z or d, find the average of the list of numbers they entered using doubles. 尝试在控制台中输入一个双精度的数组列表,然后当用户以ctrl-z或d结束arraylist输入时,找到使用双精度输入的数字列表的平均值。

I'm getting lots of errors from this and im not sure why! 我从中得到很多错误,我不确定为什么! any and all help is appreciated as I am a java beginner 感谢所有帮助,因为我是Java初学者

public static void main(String[] args) {
    ArrayList<Double> numbers = new ArrayList<Double>();  

    Scanner in = new Scanner(System.in);   

    while (in.hasNextDouble())  
    {  
        Double input = in.nextDouble();  
        numbers.add(input);  
    }  

    in.close();

    for (Double element : numbers)  {  
        Double sum += element;
        System.out.println(sum/numbers.size()); 
    }
} 

To compute the final mean: 要计算最终均值:

double sum = 0;
for(double d : numbers) {
    sum += d;
}
System.out.println(sum / numbers.size()); 
// don't miss the imports
import java.util.ArrayList;
import java.util.Scanner;

// you might have missed to add your main() method in a class
public class Average {
    public static void main(String[] args) {
        // This part was ok
        ArrayList<Double> numbers = new ArrayList<Double>();
        Scanner in = new Scanner(System.in);
        while (in.hasNextDouble()) {
            Double input = in.nextDouble();
            numbers.add(input);
        }
        in.close();

        // Forgot to initialize the value of sum
        Double sum = 0d;
        for (Double element : numbers) {
            // add the element
            sum += element;
            // don't count the average here
        }
        // count it outside the loop
        System.out.println(sum / numbers.size());
    }
}
Double sum += element;

That isn't legal syntax, you're trying to add something to a variable definition, which doesn't work. 那不是合法的语法,您正在尝试向变量定义中添加某些内容,这是行不通的。 You're close though. 不过你近了。 Just declare the variable sum outside of the for loop. 只需在for循环外声明变量sum

double sum = 0;
for (double element : numbers)  {  
       sum += element;
   }
System.out.println(sum/numbers.size()); 

You also probably don't want to print out the current average every time so you should just move that to after the loop. 您也可能不想每次都打印出当前平均值,因此您应该将其移动到循环之后。

i have added java.util package which is required for Scanner and collections classes in your code main problem was in your for loop plz mark answer . 我在代码中添加了Scannercollections classes所需的java.util包,主要问题是在for循环中,请标记为answer。 thank you :) 谢谢 :)

import java.util.*;
    public class Test {

        public static void main(String[] args) {
            Double sum=0.0;
              ArrayList<Double> numbers = new ArrayList<Double>();  

              Scanner in = new Scanner(System.in);   

              while (in.hasNextDouble())  
              {  
                 Double input = in.nextDouble();  
                 numbers.add(input);  
              }  

               in.close();


                 for (Double element : numbers)  {  
                    sum += element;
            }
                   System.out.println(sum/numbers.size()); 

              } 

    }

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

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