If 语句在未达到 working/being 的情况下

If statement in cases not working/being reached

我目前有一个继续获取输入的程序,它最初将 b 的值设置为 TRUE(即 b = 1)。然后 switch 语句启动,将 c 的值设置为 TRUE(即 c = 1)。用户的下一个输入将 b 的值设置为 FALSE,但由于某种原因从未达到第一个 if 语句,因为行 "mvprintw(22,24,"It has reached it");" 从未打印在我的屏幕上,尽管 b 的值是为假 (b = 1),而 c 的值现在为真 (c=1)。

我试过使用嵌套的 if 而不是 case,但这会使事情进一步复杂化,坦率地说,这是行不通的。任何关于此事的意见将不胜感激!

int moveC(int y, int x, int b, int i)
    { //first input from user, b is True, so first case occurs
      //second input from user, b is false, so second case occurs, however, the if first if statement is never reached, but the second one is
        int c = FALSE;

        switch(b)
        {
             case TRUE:
                 c = TRUE; //this part is first reached from initall user input
                 refresh();
                 mvprintw(26,26,"value of c is... %d",c);
                 break;

             case FALSE:
                 if(c == 1) //this part is never reached, even though the second user input is (b = 0 i.e false, and c = 1, i.e true)
                 {
                      mvprintw(22,24,"It has reached it");
                      mvprintw(y,x+7,"^");
                      refresh();
                      break;
                 }

                 else if(c == 0) //this if statement is always used even if c is not zero
                 {
                    mvprintw(y,x,"^");
                    refresh();
                    break;
                 }

moveC() 你声明

int c = FALSE;

这使得它成为驻留在堆栈中的自动变量,因此每次调用时,都会创建 c 并再次使用 FALSE 进行初始化,并且 [=16= 中的条件 c == 1 ] 永远不可能成真。如果你想在 moveC() 的第二次调用中拥有你在第一次调用中分配的值,你必须声明它

static int c = FALSE;