Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        2年前关闭。
                                                                                            
                
        
嗨,我是一个初学者C程序员,我正在尝试制作一个应该根据其他变量来更改变量的过滤器,但是由于某种原因它似乎并未更改变量g1。我真的很感激我现在所能得到的任何帮助。谢谢

#include <stdio.h>
#include <stdlib.h>
#include <time.h>


int main(void)
{
    int car;
    int g1;
    int g2;
    int sel1;
    unsigned int iseed = (unsigned int)time(NULL);
    srand (iseed);
    g1 = 0;
    car = rand() % 3 + 1;
    printf("%d\n",car);

    printf("select a door beetween 1 and 3\n ");
    scanf ("%d",&sel1);
    if (sel1 < 0)
    {
        printf("Invalid selection!\n");
        return 0;
    }
    if (sel1 > 3)
    {
        printf("Invalid selection!\n");
        return 0;
    }

    if (sel1 == 1)
    {
        if(car == 1)
        {
            g1 = 2;
            g2 = 3;
        }
        if (car == 2);
        {
            g1 = 3;
            g2 = 1;
        }
        if (car == 3);
        {
            g1 = 2;
            g2 = 1;
        }
    }

    if (sel1 == 2)
    {
        if(car == 1)
        {
            g1 = 3;
            g2 = 2;
        }
        if (car == 2);
        {
            g1 = 1;
            g2 = 3;
        }
        if (car == 3);
        {
            g1 = 1;
            g2 = 2;
        }
    }

        if (sel1 == 3)
    {
        if(car == 1)
        {
            g1 = 2;
            g2 = 3;
        }
        if (car == 2);
        {
            g1 = 1;
            g2 = 3;
        }
        if (car == 3);
        {
            g1 = 1;
            g2 = 2;
        }
    }

    printf("Behind door %d is a goat\n",g1);



    return 0;
}


显然,我没有足够的细节,所以忽略此即时消息,只是添加一些随机细节。在基础数学中,变量是代表数字的字母字符,称为变量的值,该变量可以是任意的,未完全指定的或未知的。

最佳答案

我注意到您没有指定sel1==0会发生什么
您对sel1>3sel1==3sel1==2sel1==1sel1<0进行了特殊化,但是您没有指定sel1==0的含义,我认为如果sel1为0,则g1不会更改,否则会更改
可能是您在输入sel1时应放置一个空格,如下所示

scanf ("%d ",&sel1);


并在条件;if(car==2)之后删除if(car==3)

09-09 21:08