无法运行第二个任务

时间:2014-10-26 22:02:13

标签: c

我无法选择第二项任务。如果我运行第一个任务,没有问题,我得到了结果。但我在第二和第三个任务中遇到了一些问题。即使我选择了第二个或第三个任务,程序也会要求我输入一个在第一个任务中使用的整数。这是我的代码:

#include <stdio.h>
main(void)
{

int n;
int length, row = 0, column = 0, space;     // Variables of first program
int top, bot, a, b;
int top2, bot2, a2, b2;

printf("Please select the task: ");
scanf("%d", &n);

if(n = 1)
{
    printf("Please enter an integer: ");            // Program asks the length
     scanf("%d", &length);
    row = 0;
    column = 0;
    while(row < length)                             // While loop for newline
    {
        while(column < length)                      // While loop for stars
        {
            printf("*");
            column++;
        }
        printf("\n");                               
        column = 0;                                 // Resetting the value of column variable
            while(column < row+1)                   // While loop for space
            {
                printf(" ");
                column++;
            }
            column = 0;
                row++;
    }
}   
else if(n = 2)
{
    printf("Please enter the length of top:");
        scanf("%d", &top);
    printf("Please enter the length of bottom:");
        scanf("%d", &bot);

        a = top;
        b = top;

    while(a <= bot)
    {
        while(b <= a+2)
        {
            printf("*");
            b++;
        }
        b = top;
        printf("\n");
        a++;
    }
}
    else
{
    printf("Please enter the length of top:");
        scanf("%d", &top2);
    printf("Please enter the length of bottom:");
        scanf("%d", &bot2);

        a2 = top2;
        b2 = top2;

    while(a2 >= bot2)
    {
        while(b2 >= a2)
        {
            printf("*");
            b2--;
        }
        b2 = top2;
        printf("\n");
        a2--;
    }
}
return 0;
} 

1 个答案:

答案 0 :(得分:3)

if(n = 1)

=是assign运算符。 要进行比较,请使用==

你正在做的是将n设置为1,并隐式检查该值是否不是0.所以你总是输入if分支,而不是其他分支。