Help with arrays

rowlandsfc

Active member
Joined
Feb 3, 2019
Messages
43
Location
bridgend
Programming Experience
Beginner
im new to this, im currently in my first year of college studying coding and we have tro create the game hammurabi, im having issues getting my array towork as i need it, i need to use to store data from each year(button click = 1 year) up to 10 years. the data is calculated based ontheinformation theuser enters before clicking the button.

C#:
listBox1.Items.Clear();

double[] starved = new double[10];

for (int year = 0; year != (starved.Length); year++)
{
  starved[year] = died;
  listBox1.Items.Add(starved[year]);
}

that is what ive got so far, but every time i click the button all 10 slots show the data for that yearbuti need to only show 1 year in 1 slot and not useup all10 slots every year

any helpwould be appreciated
 
You have a loop that goes from 0 to 9 so of course you're filling the whole array. Loops are for doing something multiple times so if you want to do something only once then what is the loop for? You obviously need to know which element to set so that means storing the array index in another field. Each time you click the Button, you set the element at the current index and then increment the index.
 
You have a loop that goes from 0 to 9 so of course you're filling the whole array. Loops are for doing something multiple times so if you want to do something only once then what is the loop for? You obviously need to know which element to set so that means storing the array index in another field. Each time you click the Button, you set the element at the current index and then increment the index.

i dont really understand what you mean
 
I need it to save the data of died(each button click) over 10 years so 10 clicks and then calculate the average and also use it in a chart
 
Please answer the question I actually asked. I didn't ask about 10 clicks. This is part of the problem. If you can't focus on a specific task then you end up with code that doesn't do what you want. Please think about EXACTLY what you want to happen when the user clicks the Button.
 
Back
Top Bottom