Re Tutorial 229 - Summing elements of arrays

0 Rick Reiter · April 14, 2015
I don't get a single line. For code :
 public static void main(String[] args){
           
           int bucky[]={21,16,86,21,3};
           int sum=0;
           
           for(int counter=0;counter<bucky.length;counter++){
               sum+=bucky[counter];
           
                System.out.println("The sum of these numbers is " +sum);
           }
        }
}
I get :
run:
The sum of these numbers is 21
The sum of these numbers is 37
The sum of these numbers is 123
The sum of these numbers is 144
The sum of these numbers is 147
BUILD SUCCESSFUL (total time: 0 seconds)



Post a Reply

Replies

Oldest  Newest  Rating
0 Dol Lod · April 14, 2015
That is correct, what don't you get?

The System.out.println is located within the for loop making it print numbers out each time. 

On a side note, I would be using an extended for loop here for simplicity. 
  • 1

Java / Android Development

106,968 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator