求和ArrayList值

时间:2013-09-24 16:27:38

标签: java arraylist

好的,到目前为止,我的程序允许用户输入,识别数组列表中存储的数量,然后输出存储的数字。如何汇总用户输入的所有数字?

import java.util.Scanner;
import java.util.ArrayList;

public class lab5 
{
    public static void main (String[]args)
    {
        Scanner userInput = new Scanner(System.in);

        ArrayList<Integer> list = new ArrayList<>();

        //Creates initial value for nextInt variable
        int nextInt = -1; 

        //Create loop to store user input into array list until 0 is entered
        while((nextInt = userInput.nextInt()) != 0)
        {
            list.add(nextInt);
        }

        //Calculate average once zero is inputted
        if (nextInt == 0)
        {
            System.out.println("There are " + list.size() + " numbers in this array list");
            System.out.println("The numbers are " + list);
            System.out.println("The sum of the number are ")
        }
    }
}

1 个答案:

答案 0 :(得分:0)

你很亲密!输入到您的程序的所有数字将在某个时间位于nextInt。你如何利用这个事实得到总和?提示:您需要跟踪while循环之外的总和。