## 4 replies

Last post Jan 22, 2020 11:40 AM by RichardLaw

• RichardLaw

Member

229 Points

681 Posts

### Bit of help with some basic logic

Hi,

I have a MainScore integer which will only ever be one of the following numbers - 2,4,8,15, 20.

I want to run a test on another integer checkBob (lest say the value is 9) with the following logic. If checkBob is less than 10, increase MainScore by two steps in the number sequence. If checkBob is greater than 10, increase MainScore by one step in the sequence.

So in this example, if MainScore was originally 4, it would change to 15. (increase by 2 steps in the sequence)

How do I write this in C#.net

Many thanks

• mgebhard

All-Star

50551 Points

22420 Posts

### Re: Bit of help with some basic logic

Homework?

```    class Program
{
private static List<int> nums = new List<int> { 2, 4, 8, 15, 20 };
static void Main(string[] args)
{
int currentValue = 4;
int currentIndex = nums.IndexOf(currentValue);

int score = checkBob(9, currentIndex);
Console.WriteLine(score);

}

static int checkBob(int input, int index)
{

if(input < 10)
{
index = index + 2;
}
else
{
index++;
}

if (index <= nums.Count()-1)
{
return nums[index];
}
else
{
return -1;
}
}
}```

• Rion William...

All-Star

114583 Points

18503 Posts

MVP

### Re: Bit of help with some basic logic

Jan 21, 2020 04:35 PM|Rion Williams|LINK

#### RichardLaw

I want to run a test on another integer checkBob (lest say the value is 9) with the following logic. If checkBob is less than 10, increase MainScore by two steps in the number sequence. If checkBob is greater than 10, increase MainScore by one step in the sequence.

Okay, let's walk through the parts here, you need:

• Something to store and access values of your sequence (which won't change)
• A mechanism to find where your current item is within your sequence.

A function might look something like this:

```public static int UpdateStore(int score)
{
var sequence = new int[]{ 2, 4, 8, 15, 20 };

// Lookup your current value in the sequence
var currentIndex = sequence.IndexOf(score);

// Determine how much to increment by (2 if less than 10, otherwise 1)
var increment = (score < 10) ? 2 : 1;

// Output the value from the new index
return sequence[currentIndex + increment];
}```

One consideration would be, what do you do if you receive 20? Since you wouldn't be able to increase the index by one more?

• samwu

Contributor

3360 Points

1409 Posts

### Re: Bit of help with some basic logic

Hi RichardLaw,

#### RichardLaw

I want to run a test on another integer checkBob (lest say the value is 9) with the following logic. If checkBob is less than 10, increase MainScore by two steps in the number sequence. If checkBob is greater than 10, increase MainScore by one step in the sequence.

You can try to use IndexOf method. here a demo for you as a reference.

```static void Main(string[] args)
{
List<int> data = new List<int> { 2, 4, 8, 15, 20 };
int index = data.IndexOf(number);
if (number > 10)
{
index++;
}
else
{
index = index + 2;
}
Console.WriteLine(data[index]);
}```

Best regards,

Sam

MSDN Community Support
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
• RichardLaw

Member

229 Points

681 Posts

### Re: Bit of help with some basic logic

`int currentIndex = Array.IndexOf(sequence, score);`