选择并移动数字

时间:2016-06-12 05:47:23

标签: c

我正在构建一个c程序来选择和移动数组中的数字。我的目标是在数组中连接2对相同的数字。但我不确定为什么选定的号码无法移动。需要帮助,提前谢谢。

这是我的代码:

void playgame(char box[ROW][COL])
{
  int x, y, choice2,num,direction=0;
  char input;

  do{

     printf("Please select a number (1-7) : ");
     scanf("%i",&num);

     if(num==0 ||num > 7)
     {

     printf("Invalid!\n");


     }
     else
     {

     printf("\nNumer %i is currently selected!\n", num);

     }
    }while(num==0 ||num > 7);

    printf("\n[1]Move\n[2]Sign out\n");
    printf("\nEnter choice: \n");
    scanf("%d", &choice2);


    switch(choice2)
    {
      case 1: 
                {
                    printf("Press 'e' to go up\n");
                    /*codes for moving the character up.....*/
                }

                {
                    printf("Press 'd' to go right\n");

                }

                {
                    printf("Press 's' to go left\n");

                }

                {
                    printf("Press 'x' to go down\n");

                }

                    fflush(stdin);

                    scanf("%c", &input);
                break;

        case 2: printf("Bye!\n");
    }



  for(x=0; x<9; x++)

  for(y=0; y<9; y++)
  {     
    if(input == 'e')
  if(box[x][y]==num)
  {
   box[--x][y]==num;
   }

 if(input == 'd')
  if(box[x][y]==num)
  {
   box[x][++y]==num;
   }


 if(input == 's')
    if(box[x][y]== num)
    {
        box[x][--y]== num;
    }

if(input == 'x')
  if(box[x][y]==num)
  {
   box[++x][y]==num;
   }
     }


}

例如: numbers

1 个答案:

答案 0 :(得分:3)

您混淆了“等于”运算符==和“赋值”运算符=

if(box[x][y]== num)这样的所有行都是正确的:你在比较它们是否相等。

box[++x][y]==num;之类的所有行都需要更改为box[++x][y] = num; - 您希望将“{”分配给box条目,而不是“比较”它。

但是你还遇到了另一个问题:在所有行box[++x][y] = num;中你不想使用++x--y - 这些实际上会改变x和{ {1}},我认为你不想要。 y循环正在forxy移动到0 - 使用9--x更改这些++y循环值。

相反,您希望forbox[x+1][y] = num;之类的内容计算“超过box[x][y-1] = num;”和“小于x”,并使用计算出的值,而不是正在更改yx

但是,如果你这样做,你甚至会遇到更多问题:y y-1已经y是开箱即用的问题。0 x+1已经x的时候9是开箱即用的FileUploadHash UserID Safe Virus FileName Blobdata -------------- ------- ---- ----- ---------- --------- SHA256 Hash ###### 0 1 xyajss.pdf {bin vomit} SHA256 Hash ###### 1 0 2021jf.png {bin vomit} 。你需要对你的数学更加谨慎。