首页 > 解决方案 > 为什么使用设置参数调用函数不会产生预期的输出?

问题描述

在下面的代码中,当我调试它时,第一个调用函数 (mazesentence(0, 0, 1, 0);) 也去了 if(before == 1 ),但 'before' 显然是零。为什么会进入那个位置?我想,我的牙套不正确。

#include <stdio.h>

void mazesentence(int little, int twist, int twisting, int before)
{    
    
    
    if(little == 0 && twist == 0 && twisting == 1)
    {
        if(before == 0)
        {
            if(twisting == 0)
                printf("twisty ");
            else if(twisting == 1)
                printf("twisting ");
            printf("little ");
            printf("maze of ");
        }
        if(before == 1)
            printf("little ");
        {
            if(twisting == 0)
                printf("twisty ");
            else if(twisting == 1)
                printf("twisting ");
                printf("maze of");
        }
        
    }
    return;
}

int main( )
{
    mazesentence(0, 0, 1, 0);
    mazesentence(0, 0, 1, 1);
    mazesentence(0, 0, 0, 0);
    mazesentence(0, 0, 0, 1);
    mazesentence(0, 1, 1, 0);
    mazesentence(0, 1, 0, 0);
    mazesentence(1, 0, 1, 0);
    mazesentence(1, 0, 0, 0);
    mazesentence(1, 1, 1, 0);
    mazesentence(1, 1, 1, 1);
    mazesentence(1, 1, 0, 0);
    mazesentence(1, 1, 0, 1);

    return 0;
}

标签: cxcode

解决方案


你的牙套确实不正确。有条件地看问题:

        if(before == 1)
            printf("little ");
        {

就是这样,if唯一的守护者printf("little ")。然后是一个无条件执行的代码块。

应该是这样守护整个区块:

        if(before == 1)
        {
            printf("little ");

推荐阅读