我目前正在编写一个程序,该程序从用户处获取他们想要在数组中有多少个数字以及这些数组中有哪些数字来比较这两个数字以在相交处找到其并集的输入。

我写了这篇文章,但是由于某种原因,用户在第一个数组(a)中输入了他们想要的数字以及哪些数字后,它跳过了第二个数组(b)的整个用户输入。

联合和相交的计算是正确的(未显示),但我只是无法弄清缺少的内容。我是C的新手,所以可能缺少一些小问题。

谢谢你的帮助!

int main(void){

    int i, j, x, y;
    int elemA, elemB;
    int a[10] = {0};
    int b[10] = {0};


    // Prompts user to enter the amount of numbers that will be in array a
    // then asks user to enter the values (0-9) to be inputted.
    printf("Enter the number of elements in set A: \n");
       scanf("%d", &elemA);
    printf("Enter %d number(s) for set A: \n", elemA);
       scanf("%d", &x);
       if(x < 10)
          a[x]=1; // sets the index in the array to 1 if the
                  //corresponding number that has been inputted


    // Prompts user to enter the amount of numbers that will be in array a
    // then asks user to enter the values (0-9) to be inputted.
    printf("Enter the number of elements in set B: \n");
    scanf("%d", &elemB);
    printf("Enter %d number(s) for set B: \n", elemB);
       scanf("%d", &y);
       if(y < 10)
          b[y]=1; // sets the index in the array to 1 if the
                  //corresponding number that has been inputted

*** rest of code ***

最佳答案

这里:

printf("Enter %d number(s) for set A: \n", elemA);
scanf("%d", &x);

您仅读取一个int,其他int已排队,并且将在您下次使用scanf时使用,而无需用户输入。

关于c - C程序跳过了用户输入?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35261948/

10-09 15:12
查看更多