Adding numbers in an array?
Hello. I have this problem. Here is the code first
class SalesAnalysis
{
private String [] months = {"", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "July", "Aug", "Sep", "Oct", "Nov", "Dec"};
private int [] sales2005 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
private int [] sales2006 = {0, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
public void getTotal (int[])
{
}
Question: This method [getTotal (int[]) ]will total all the values in an array. A parameter to this method is a reference to an array of integers.
The method will return the total of the values in the referenced array.
I think the code may be something like this:
for (int i = 0; i < sales2005.length; i++)
System.out.print(sales2005[i])
Any ideas? I am stumped. I have an exam this week.
uh.... that's hard to believe...
If you came up with the code in your first post, then you surely can figure out what Fubarable and myself suggested.
CJSL
Arrays and adding: (Some progress)
public class TestSalesa
{
public static void main (String [] args)
{
int i = 0;
int sum = 0;
String [] months = {"", "Jan", "Feb"};
int[] sales2005 = {0, 20, 30};
int[] sales2006 = {0, 40, 50};
getTotal(sales2005);
}
static void getTotal(int [] sales2005)
{
int sum = 0;
for (int i = 0; i < sales2005.length; i++)
sum = sum + sales2005[i];
System.out.print(sum);
}
}
I worked this out. but i cant write a driver to call the getTotal. arrgh it wont work.