- Home /
Calculating the total of an int Array
public static int[] jbScores = {0,2,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
public static int grandTotalJellyBeans;
How would I put the grand total of "jbScores" into a single Int so the answer would be 5
Comment
Answer by gwubgwub · Apr 06, 2014 at 07:08 PM
I dunno why but that didn't work for me I tried a for loop and it did what I wanted it to do.
public static int[] jbScores = {0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
public static int grandTotalJellyBeans;
void JellyBeansTally()
{
for (int i = 0; i < jbScores.Length; i++)
{
grandTotalJellyBeans += jbScores[i];
}
}
Because you have added amount of elements to grandTotalJellyBeans.