从数组中始终取两个值

时间:2019-05-22 09:41:58

标签: c#

是否有可能我总是以这种形式从该数组中提取两个值: 12,34,56,78,910?

我现在可以得到这个结果
enter image description here

这是我的代码:

  int[] array1 = new int[] { 1,2,3,4,5,6,7,8,9,10 };
            for (int i = 0; i < array1.Length; i++)
            {
                try
                {
                    Console.WriteLine(array1[i] + "  " + array1[i+1]);

                }
                catch (System.IndexOutOfRangeException)
                {
                   System.Console.WriteLine(array1[i]);
                    Console.ReadLine();
                }
            }

4 个答案:

答案 0 :(得分:0)

def ai_player(self, state, team = 1): 
    new_shape_x = np.asarray(state[1]).shape
    player1 = Minimax(n = new_shape_x, default_team = team)
    if team == -1:
        state = player1.convert_board_state(state)
    best_move = player1.decision_maker(state)
    chosen_succ, utility = best_move
    if team == -1:
        chosen_succ = player1.convert_board_state(chosen_succ)
    return chosen_succ

答案 1 :(得分:0)

 int[] array1 = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        for (int i = 0; i < array1.Length; i++)
        {
            try
            {
                Console.WriteLine(array1[i] +""+ array1[i + 1]);i++;

            }
            catch (System.IndexOutOfRangeException)
            {
                System.Console.WriteLine(array1[i]);
                Console.ReadLine();
            }
        }

答案 2 :(得分:0)

我想你想要这个:

        bucket = s3.Bucket(settings.AWS_STORAGE_BUCKET_NAME)
       key = 'media/zips/' + str(self.request.user.id) + '.zip'

        objs = list(bucket.objects.filter(Prefix=key))
        if len(objs) > 0 and objs[0].key == key:
            print("Exists!")
            return JsonResponse({'status': 'success'})

        else:
            print("Doesn't exist")
            return JsonResponse({'status': 'failed'})

不要依赖 "Version": "2012-10-17", "Statement": [ { "Sid": "some_id", "Effect": "Allow", "Principal": "*", "Action": "s3:*", "Resource": [ "arn:aws:s3:::<bucket name>", "arn:aws:s3:::<bucket name>/*" ] } ] } / int[] array1 = new int[] { 1,2,3,4,5,6,7,8,9,10}; for(int i = 0; i< array1.Length; i++) { if(i+1 < array1[array1.Length-1]){ Console.WriteLine(array1[i] + " " + array1[(i+1)]); }else { Console.WriteLine(array1[i] + " " + array[0]); } } ,因为使用if / else可以轻松避免捕获此异常。

我们只是检查以确保它没有将最后一个索引与try配对。

catch语句中,您可以删除something out of bounds并随意更改它作为占位符-因为您没有使用第二列中的第一个值,因此只是将其循环返回。

答案 3 :(得分:0)

尝试一下:

int[] array1 = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int i = 0;
while(i < array1.Length-1)
{
    Console.WriteLine(array1[i++]+""+array1[i++]);
}