locked
Bit of help with some basic logic RRS feed

  • Question

  • User1953081067 posted

    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

    Tuesday, January 21, 2020 4:17 PM

Answers

  • User281315223 posted

    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)
    {
         // Store your sequence
         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?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 21, 2020 4:35 PM

All replies

  • User475983607 posted

    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;
                }
            }
        }

    Tuesday, January 21, 2020 4:35 PM
  • User281315223 posted

    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)
    {
         // Store your sequence
         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?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 21, 2020 4:35 PM
  • User288213138 posted

    Hi 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 };
                Console.WriteLine("please write a number");
                int number = Convert.ToInt32(Console.ReadLine());
                int index = data.IndexOf(number);
                if (number > 10)
                {
                    index++;
                }
                else
                {
                    index = index + 2;
                }
                Console.WriteLine(data[index]);
                Console.ReadKey();
            }

    Best regards,

    Sam

    Wednesday, January 22, 2020 3:24 AM
  • User1953081067 posted

    Thank you all so much for your time and support. I went with this option in the end, although I'm sure the others work too.

    I did need to change one line to the following to get it to work for me:

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

    Thank you again. As always, very much appreciated.

    Wednesday, January 22, 2020 11:40 AM