2

I have tried to get the point across in the title as best as I can but basically what I want to do is add certain items to List while running a loop so I don't have to manually put them into an if statement. Let me please show an example so that I can explain properly.

Example :-

What I need is :- the first number would be 500 and that would be in index 0, then i want a loop to add 150 to the last number generated so that the int list would look like this,

index 0 = 500
index 1 = 650
index 2 = 800
index 3 = 950

Do this repeatedly until say the last number will read 2,000,000

Now I believe that this would be simple to run a loop and base it on conditions but I can only seem to figure out to run a loop that will increment the value in 1.

Hope I have explained well enough

Regards, M

2
  • 1
    Would you be so kind as to show us your loop? Commented Jan 1, 2016 at 19:16
  • public void GenerateList() { listlevel = new List<int>(); for (int i = 500; i <= 2000000; i += 150) { listlevel.Add(i); } } Commented Jan 1, 2016 at 19:26

2 Answers 2

6

Now I believe that this would be simple to run a loop and base it on conditions but I can only seem to figure out to run a loop that will increment the value in 1.

This is not true, you can adjust the increment of the iterator as you wish.

var numbers = new List<int>();
for(int i=500; i<=2000000; i+=150)
{
    numbers.Add(i);
}

For further information on this, please have a look here.

Sign up to request clarification or add additional context in comments.

7 Comments

Ah that's fantastic I didn't know you could do that I should of read the documentation better, Ive put that in now and I have around 13k indexes which is perfect now, also tricky is the next step so I now have a value of say 2145 I now want to search the list for the index number that would be say lower than the next index position but higher than the one below if that makes sense?
@Marca please try to plan a solution for this and then try to implement it. If you don't make it, visit Stack Overflow and show your effort and where you got stucked :) But please define your problem crystal clear in paper and think about it. This will improve your problem solving skills.
@Christos yup I am trying to code it as we speak :) so currently trying to search the list if the number I have is greater than the one in the list and hoping at the point it stops will give me my index
public void CheckIndexNumber(int XP) { for (int i = 0; i < listlevel.Count; i++) { if(i > XP && i < XP) { Debug.Log(i); indexNumber = i; } } }
Ok I have that but it doesnt return a value yet, am i missing something?
|
1

Just another implementation:

var result = new List<int>();
var number = 500;
do
{
    result.Add(number);
    number+= 150;
} while (number <= 2000000);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.