0

Ok, so far my program allows user input, recognizes how many numbers are stored in the array list and then outputs those numbers that are stored. How can I sum all the numbers entered by the user?

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 ")
        }
    }
}
4

1 回答 1

0

你很近!输入到您的程序中的所有数字都将驻留在nextInt某个时刻。你怎么能用这个事实来得到总和?提示:您需要跟踪while循环外的总和。

于 2013-09-24T16:32:36.547 回答