如果语句保持循环 - c

Cod*_*ers 0 c loops if-statement function

所以我试图完成这个程序,所有我需要做的是为这个纸牌游戏实现某种形式的用户输入,但到目前为止我尝试过的所有东西都是无休止地循环(或者直到我用尽阵列中的元素)而且当我查看我的代码时,我不知道我做错了什么,这似乎是有道理的.

void players(int deck[])
{
    int x;
    int a; 

    a = 1;

     printf("Player 1 \n"); 
     printf("Your Hand is: \n"); 
     draw(deck, a);
     draw(deck, a);
     while(a = 1)
     {
     printf("What would you like to do: Press 1 to Draw. 2 to Stay. \n"); 
     scanf("%d" , &x); 
     if(x = 1)
     {
          draw(deck, a);
     }
     else
     {
         a--;
     }
     }
}
Run Code Online (Sandbox Code Playgroud)

这是有问题的输入

void draw(int deck[SIZE], int a)
{
    int numCards = 10;
    int i; 
    int hand[numCards];
    int card;
    for(i = 0; i < numCards && top > 0; i++)
    {
        card = deck[top-1];     
        hand[i] = card; 
        top--;   
    }
    if(a != 0)
    printcards(card);
    else
    for(i = 0; i < numCards && top > 0; i++)
    printcards(card);

}
Run Code Online (Sandbox Code Playgroud)

这是Loop用来绘制卡片的功能(打印卡是一个单独的功能,仅打印出卡片)玩家调用绘图并且它可以工作但是如上所述,即使我按下2(假设要退出)它也会无休止地调用卡片.所以我不完全确定我做错了什么.

Mic*_*gan 6

while(a = 1)

分配1a将永远导致此循环.您应该使用==相反的方式进行比较.请打开编译器警告并修复它们,因为它们很容易发现这种情况.

顺便说一下,你的if(x = 1)陈述也有同样的错误......