我正在构建一个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
答案 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
循环正在for
和x
从y
移动到0
- 使用9
和--x
更改这些++y
循环值。
相反,您希望for
或box[x+1][y] = num;
之类的内容计算“超过box[x][y-1] = num;
”和“小于x
”,并使用计算出的值,而不是正在更改y
或x
。
但是,如果你这样做,你甚至会遇到更多问题: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}
。你需要对你的数学更加谨慎。