简体   繁体   English

遍历用户输入和C中的多维arrays

[英]Going through user input and multi-dimensional arrays in C

I am trying to make a code where a user has 5 tries to guess a number and if any of the 3 series of numbers within Winning_order then both of the for loops will break.我正在尝试编写一个代码,其中用户有 5 次尝试猜测一个数字,如果Winning_order中的 3 个数字系列中的任何一个,那么两个 for 循环都会中断。 The usersInputs stores the users inputs to be compared with Winning_order . usersInputs存储要与Winning_order进行比较的用户输入。 So for example, if the number 1,2,3 or 1,2,4,5,3 is inputted by the user the loop will print There is a Correlation and the for loops will stop.因此,例如,如果用户输入数字1,2,31,2,4,5,3 ,循环将打印There is a Correlation并且 for 循环将停止。 If the input is 7,8,9,3,2 since no 3 numbers are present within the Winning_order the loops will just stop.如果输入为7,8,9,3,2 ,因为Winning_order中不存在 3 个数字,循环将停止。 There is a problem with the match_arrays function and I do not know how to go about stopping the nested for loops if the if statement is valid. match_arrays function 有问题,如果if statement有效,我不知道如何停止嵌套 for 循环 go。

Checking if the function has a correlation检查 function 是否有关联

int match_arrays(int *arr1, int *arr2, int len) {
  for (int p = 0; p < len; p++) {
    if (arr1[p] != arr2[p]) {
      return 0;
    }
  }
  return 1;
}

main() function主要()function

int main(void)
{
    int Winning_order[3][3] = {{1,2,3}, {1,4,7}, {2,5,8}};
    int input = 0;
    int usersInputs[5] = {0};
    for (int i = 0; i <= 4; i++){
           printf("\nPlayer input: ");

           scanf("%d", &input);
           usersInputs[i] = input;
           for (int p = 0; p < 5; p++) {
                if (match_arrays(usersInputs, Winning_order[p], 3)) {printf("There's a Corelation");}
    }}
    return 0;
}

There are different ways of approaching breaking a double loop (or to generalize: a nested loop), this solution is not the optimal, the best, or the recommended, but it works.有多种方法可以打破双循环(或概括地说:嵌套循环),此解决方案不是最佳、最佳或推荐的解决方案,但它有效。 I fixed the indentation and some naming issues, but I won't fix anything else.我修复了缩进和一些命名问题,但我不会修复任何其他问题。

This is a working snippet:这是一个工作片段:

#include <stdio.h>

int match_arrays(int *arr1, int *arr2, int len) {
  for (int p = 0; p < len; p++) {
    if (arr1[p] != arr2[p]) {
      return 0;
    }
  }
  return 1;
}

int main(void) {
  int winning_order[3][3] = {{1,2,3}, {1,4,7}, {2,5,8}};
  int input = 0;
  int users_inputs[5] = {0};
  for (int i, break_i = 0; !break_i && i < 5; i++){
    printf("\nPlayer input: ");
    scanf("%d", &input);
    users_inputs[i] = input;
    for (int p = 0; p < 3; p++) {
      if (match_arrays(users_inputs, winning_order[p], 3)) {
        printf("There's a Corelation\n");
        break_i = 1;
        break;
      }
    }
  }
  return 0;
}

I made this easy for you to understand.我让这很容易让你理解。 I added a new flag to the outer for loop called break_i with an initial falsy value.我在名为break_i的外部for循环中添加了一个新flag ,其初始值为假值。 Simultaneously, I added a short circuit && operation to the for loop.同时,我在for循环中添加了短路&&操作。

Inside your inner loop, I added a break_i = 1 statement that will make the outer loop stop.在你的内部循环中,我添加了一个break_i = 1语句,这将使外部循环停止。 Immediately after that, I use the break statement to break the inner loop.紧接着,我使用break语句来中断内部循环。


PS: I also fixed the index out of bounds pointed out by @kaylum. PS:我还修复了@kaylum 指出的索引越界。 You may want to use a macro or sizeof() next time, but that's beyond the scope of your question.下次您可能想使用宏或sizeof() ,但这超出了您问题的 scope 范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM