java - Summing ArrayList values -


ok, far program allows user input, recognizes how many numbers stored in array list , outputs numbers stored. how can sum numbers entered 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 nextint variable         int nextint = -1;           //create loop store user input array list until 0 entered         while((nextint = userinput.nextint()) != 0)         {             list.add(nextint);         }          //calculate average once 0 inputted         if (nextint == 0)         {             system.out.println("there " + list.size() + " numbers in array list");             system.out.println("the numbers " + list);             system.out.println("the sum of number ")         }     } } 

you're close! numbers inputted program reside in nextint @ point. how can use fact sum? hint: you'll need keep track of sum outside of while loop.


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -