C# Loop Array Index Values back to Beginning of Array once out of Index

后端 未结 2 971
梦如初夏
梦如初夏 2021-01-20 05:25

I am looking to create a program like the following (c# btw):

int[] arr = new int[9]
//some code that puts values 1, 0, or 2 in each array element
for(int i          


        
相关标签:
2条回答
  • 2021-01-20 05:46

    Use the modulo operator like this:

    if (arr[i] == arr[(i + 3) % arr.Length]) { return true; }
    
    0 讨论(0)
  • 2021-01-20 06:06

    You could try the following expression inside your if statement.

    arr[i] == arr[(i + 3) % arr.Length];
    

    The % Operator

    Divides the value of one expression by the value of another, and returns the remainder.

    0 讨论(0)
提交回复
热议问题